2

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

2 Answers 2

2

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 A

This 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.

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

3 Comments

how did the position of YOU and ARE change?
i'm just starting with a book. complete newbie.
While your conclusion is correct the breakdown is still wrong. (first (rest A)) is 'ARE not 'YOU and (first (rest (rest A))) is 'YOU not 'ARE
1

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> 

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.