0

I would like to ask why does my function not require to use the values function in order to output a nested list?

(defun p2 (l)
(cond
 ((null l) nil)
 ((listp (car l)) ( values (p2 (car l)) (p2 (cdr l))) )
 (t (princ (car l)) (princ " ") (p2 (cdr l)))
 ))

I mean doesn't the cond construct return 1 value or take 1 action if the condition is true?
And why does this work?

1
  • 1
    printing values has nothing to do with returning values. p2 returns one or two nil and that is it. princ prints regardsless of return. Commented Apr 7, 2018 at 0:01

1 Answer 1

5
(defun p2 (l)
  (cond
    ((null l) ; condition
     nil)     ; body
    ((listp (car l))         ; condition
     (values (p2 (car l))    ; \
             (p2 (cdr l))))  ; /` body
    (t                ; condition
     (princ (car l))  ; \
     (princ " ")      ;  > body
     (p2 (cdr l)))))  ; /

This cond form has three clauses. Each clause is a list where the first element is a condition and the rest is a body. Cond looks at each clause in turn until it finds one where the condition is true. It then executes the body of that clause in an implicit progn. Progn returns all the values returned by the last form of its body. Cond returns all the values returned by the matching clause. A function returns all the values returned by its body (which is another implicit progn).

As to the “why”: it is defined in this (quite sensible) manner in the standard, and the implementations implement it this way.

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

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.