tags:

views:

91

answers:

2

Hello every one, 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.

+3  A: 

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)))
Rainer Joswig
+3  A: 

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.

mquander