CLISP - change a simple list

I need to change the elements of a simple (one-dimensional) list. I know that there is a built-in callback function, but I cannot use it for this.

Here is my attempt:

(defun LISTREVERSE (LISTR) (cond ((< (length LISTR) 2) LISTR) ; listr is 1 atom or smaller (t (cons (LISTREVERSE (cdr LISTR)) (car LISTR))) ; move first to the end ) ) 

The conclusion is pretty close, but it’s wrong.

 [88]> (LISTREVERSE '(0 1 2 3)) ((((3) . 2) . 1) . 0) 

So I tried using append instead of cons :

 (t (append (LISTREVERSE (cdr LISTR)) (car LISTR))) 

But this error turned out:

 *** - APPEND: A proper list must not end with 2 

Any help?

+2
source share
4 answers

I can give you a couple of pointers because it looks like homework:

  • The main case of recursion is when the pool is null, and not when the list has less than two elements
  • Consider the definition of an auxiliary function with an additional parameter, “battery”, initialized in an empty list. For each item in the original cons list, it is at the head of the battery. When the input list is empty, return the battery

As a note, the above solution is tail recursive.

+6
source

As a continuation of the work of Oscar Lopez (and the fight against the temptation, just write another solution):

  • Using both append and length makes the published solution about the least efficient way to reverse the list. Check out the cons and null documentation for some best ideas on how to implement this.
  • Please indent correctly .
  • In this case, tail recursion is both more efficient and reasonable. Try it if you haven't done it yet. labels is the form you want to use to define local recursive functions.
  • Perhaps you should flip The Little Schemer . This will give you a better idea of ​​recursion in general.
+3
source

Good thing you did. You just skipped the list of results.

Think about it: you need to add a 1-element CAR list to the end of the reverse CDR list:

 (defun LISTREVERSE (LISTR) (cons ((< (length LISTR) 2) LISTR) ; listr is 1 atom or smaller (t (append (LISTREVERSE (cdr LISTR)) (list (car LISTR)))))) 
0
source
 (defun listreverse (list) (let (result) (dolist (item list result) (push item result)))) 
  • Do not use recursion in Common Lisp when there is a simple iterative way to achieve the same goal. Generic Lisp makes no guarantees regarding tail recursion, and calling a tail recursive function cannot be optimized for transition at the discretion of the compiler.
  • push adds an element to the result
  • dolist has an optional third argument, which is the return value. It is calculated when you exit the loop.
0
source

All Articles