I am new to LISP and cant figure out What does the following LISP does?
(setq A '(RIGHT ARE YOU))
(print (reverse (list (first (rest A))(first (rest (rest A))) (first A) 'HOW)))
setq assigns lexical variables
It prints:
(HOW RIGHT YOU ARE)
The first line assigns a list of the 3 elements to the symbol A. This is quoted to prevent evaluation of the (RIGHT ARE YOU) as a function called RIGHT. The second line does some needlessly verbose and complex logic to basically create a list consisting of four elements: the string HOW and the three elements from A.
Breaking down the second line:
(first (rest A)) - this returns the element YOU from A(first (rest (rest A))) - this returns the element ARE from A(first A) - this returns the element 'RIGHT' from AThis now leaves you with:
(print (reverse (list ARE YOU RIGHT 'HOW)))
Which LISP are you learning? Many LISP's have a notion of a REPL (read-eval-print loop) which lets you experiment with complex expressions and break them down into smaller chunks to understand the results of intermediate steps.
Perhaps this transcript of a REPL session in Common Lisp will enlighten:
CL-USER> (setq a '(right are you))
(RIGHT ARE YOU)
CL-USER> (print (reverse (list (first (rest a)) (first (rest (rest a))) (first a) 'how)))
(HOW RIGHT YOU ARE)
(HOW RIGHT YOU ARE)
CL-USER> a
(RIGHT ARE YOU)
CL-USER> (rest a)
(ARE YOU)
CL-USER> (cdr a)
(ARE YOU)
CL-USER> (first (rest a))
ARE
CL-USER> (cadr a)
ARE
CL-USER> (rest (rest a))
(YOU)
CL-USER> (cddr a)
(YOU)
CL-USER> (first (rest (rest a)))
YOU
CL-USER> (caddr a)
YOU
CL-USER> (first a)
RIGHT
CL-USER> (car a)
RIGHT
CL-USER> (values (first (rest a)) (first (rest (rest a))) (first a) 'how)
ARE
YOU
RIGHT
HOW
CL-USER> (list (first (rest a)) (first (rest (rest a))) (first a) 'how)
(ARE YOU RIGHT HOW)
CL-USER> (list 'are 'you 'right 'how)
(ARE YOU RIGHT HOW)
CL-USER> (reverse '(are you right how))
(HOW RIGHT YOU ARE)
CL-USER> '(how right you are)
(HOW RIGHT YOU ARE)
CL-USER> (print '(how right you are))
(HOW RIGHT YOU ARE)
(HOW RIGHT YOU ARE)
CL-USER>