2

I wrote a test function to test my understanding of "return-from" in Lisp

(defun testp (lst)
  (mapc #'(lambda (x y)
            (if (null lst)
                (return-from testp t)))
        lst
        (cdr lst)))

I think the test (testp 'nil) should return T but it returns NIL. Could you please help my understanding of why it returns NIL?

Many thanks.

1
  • 1
    You may get more help with your future questions if you accept some answers to your previous ones. Commented Dec 3, 2009 at 16:53

2 Answers 2

3

You call MAPC over two empty lists.

How should the LAMBDA function ever be used if the lists don't have any elements to map over?

Btw., you can write 'list' instead of 'lst'.

(defun testp (list)
  (mapc #'(lambda (x y)
            (if (null list)
                (return-from testp t)))
        list
        (cdr list)))
Sign up to request clarification or add additional context in comments.

Comments

3

Normally, mapc would apply your lambda to each element of a list. My guess (I don't use Common Lisp) is that since mapc has no elements in the list to operate on, your lambda never gets called at all, and as a result the return value of your function is the return value of mapc, which (since it mapped over nothing) is nil.

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.