Reputation: 6833
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.
Upvotes: 2
Views: 356
Reputation: 71937
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
.
Upvotes: 3
Reputation: 139241
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)))
Upvotes: 3