0

I wrote a code to reverse a list and all its sublists recursively :

(defun rev1( list final )
    ( if ( eq list () )
       final
       ( if ( atom ( car list ) )
           ( rev1( cdr list ) ( cons ( car list ) final ) )
           ( rev1( cdr list ) ( cons ( rev1( car list ) () ) final ) ))))

(defun rev(list)
   ( rev1 list () ) )

The problem is that if I call the function with : ( rev '( 1 2 '( 3 2 1 ) 3 ) ) the expected output should be (3 (1 2 3) 2 1) but instead of this what I get is : (3 ((1 2 3) QUOTE) 2 1) and I do not understand why. Could anyone explain me where's the problem?

Will Ness
  • 70,110
  • 9
  • 98
  • 181
kvway
  • 494
  • 4
  • 16
  • http://dept-info.labri.u-bordeaux.fr/~idurand/enseignement/PFS/Common/Strandh-Tutorial/indentation.html – coredump Dec 18 '16 at 11:28
  • Possible duplicate of [When to use 'quote in Lisp](http://stackoverflow.com/questions/134887/when-to-use-quote-in-lisp) – sds Dec 18 '16 at 14:23

1 Answers1

3

The ' character is a reader macro, i.e. the Lisp reader will expand it into a call to quote. The CLHS entry on quote gives the following evaluation examples:

'a  => A
''a => (QUOTE A) 

Hence:

(rev (list 'a))  =>     (A)
(rev (list ''a)) =>     ((A QUOTE))
(rev ''a)        =>     (A QUOTE)
(rev '(a))       =>     (A)
(rev '('a))      =>     ((A QUOTE))
Claus Brod
  • 131
  • 4