2

How to reverse a list such that every sublist is also reversed? This is what I have so far:

(defun REV (L)
  (cond
    ((null L) nil)
    ((listp L)
     (append
      (REV (cdr L))
      (list (car L))))
    (t
     (append
      (REV (cdr L)) 
      (list (car L))))))

4 Answers 4

6

You are on the right track, but your last two conditions have the same action, which should give an indication that one of them is not doing what it should. Indeed, the second condition, the listp case, is not right, because when it's a list, you need to append the reverse of that list instead of the unmodified list. A possible solution:

(defun my-reverse (l)
  (cond ((null l) nil)
        ((listp (car l)) (append (my-reverse (cdr l)) 
                                 (list (my-reverse (car l)))))
        (t
          (append (my-reverse (cdr l)) 
                  (list (car l))))))

>  (my-reverse '((1 2 3) (4 5 6)))
((6 5 4) (3 2 1))

As you can see, the only difference is that you test if the first element is a list, and if it is, you reverse the first element before appending it.

Sign up to request clarification or add additional context in comments.

1 Comment

A bit shorter: (defun my-reverse (l) (if (null l) nil (append (my-reverse (cdr l)) (list (if (listp (car l)) (my-reverse (car l)) (car l))))))
2

I'd write it this way:

(defun reverse-all (list)
  (loop
     with result = nil
     for element in list
     if (listp element)
     do (push (reverse-all element) result)
     else do (push element result)
     finally (return result)))

Comments

0

Sounds like a homework problem :)

Looks like you started by writing the regular reverse code. I'll give you a hint: The second condition (listp L) isn't quite right (it'll always be true). You want to be checking if something else is a list.

Comments

0

dmitry_vk's answer (which probably is faster in most lisps than using append in the previous examples) in a more lispish way:

(defun reverse-all (list)
  (let ((result nil))
    (dolist (element list result)
      (if (listp element)
          (push (reverse-all element) result)
          (push element result)))))

Or even:

(defun reverse-all (list)
  (let ((result nil))
    (dolist (element list result)
      (push
        (if (listp element) (reverse-all element) element)
          result))))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.