[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

mapcar and values interaction

    Date: Mon, 4 Mar 91 10:59:36 -0700
    From: drstrip@gnome.cs.sandia.gov (David R. Strip)

    Why does (mapcar #'(lambda (x) (values)) '(1 2 3)) eval to
    (nil nil nil) and not ()?

CLtL p. 128 - 129: "The value returned by mapcar is a list of the results of
the successive calls to the function".

In this case, mapcan will do what you want:
  (mapcan #'(lambda (x) (if (numberp x) (list x) nil)) '(1 a 2 b 3))

    In general, how do you write a function for use in a mapcar so that
    it may or may not contribute to the resulting list?

Since lisp is a functional language, every function call must return a value.
So, in short, you can't. However, you can do what I did.