1

Here is an example the Lispworks.com Hyperspec gives for mapcan:

 (mapcan #'(lambda (x) (and (numberp x) (list x)))
      '(a 1 b c 3 4 d 5))
=>  (1 3 4 5)

I'm not understanding the reason for the

(list x)

in the and form. Without it only T is returned, but why? Also, why is the #' in front of the lambda form?

2 Answers 2

1

MAPCAN function &rest lists+ => concatenated-results

MAPCAN expects the argument function to return a list. In the example the argument function calls LIST on a number, thus returns a list with one item, a number. MAPCAN combines all the result lists into a single list, then.

1

And returns its last form if all forms are non null, so if the predicate returns true, x is appended to the result.

The #' in front of lambda makes the lambda form a function. In modern Common Lisp you don't need it since there is the lambda macro that expands to the same thing.

1
  • 1
    More specifically: if x is a number, then (and (numberp x) (list x)) returns (list x) and concatenates that to the list. if x is not a number, then (and (numberp x) (list x)) returns nil, and mapcan does not append nil to the list. May 14, 2014 at 5:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.