Strange results using quote, reversing a list with all sublists recursively

56 views Asked by At

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?

1

There are 1 answers

0
Claus Brod On BEST ANSWER

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))