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

Question

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

Answers 1
Added at 2016-12-18 11:12
Tags
Question

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?

Answers to

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

nr: #1 dodano: 2016-12-18 11:12

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))
Source Show
◀ Wstecz