Search code examples
lispcommon-lisplisp-2

Why does mapcar in lisp take a name instead of function?


I am going through a lisp book and I am looking at mapcar, my question is why is that this is valid:

> (mapcar #'+ '(1 2) '(3 4))

but this one isn't:

(mapcar + '(1 2) '(3 4))

in other words, is there a reason it was decided in lisp that the first argument to mapcar cannot be the function itself, and has to be its name? what purpose does this serve?


Solution

  • is there a reason it was decided in lisp that the first argument to mapcar cannot be the function itself, and has to be its name? what purpose does this serve?

    It's just that in something like Common Lisp, the identifier + has multiple different purposes. It is a variable, a function, plus various other things.

    writing + means the variable. It is used by the read eval print loop. The value of + is the last form that was evaluated, the value of ++ is the previous value of +, and the value of +++ is the previous value of ++.

    To tell Common Lisp that you want to use the function value of an identifier +, one has to write (function +) or shorter #'+.

    Thus

    (mapcar (function +) '(1 2) '(3 4))
    

    or shorter

    (mapcar #'+ '(1 2) '(3 4))
    

    actually means call mapcar with the function + and the lists (1 2) and (3 4)

    There are two other ways to use the function +.

    (mapcar '+ '(1 2) '(3 4))
    

    Above will have Lisp retrieve the global function value of the symbol +.

    Fourth, we can also have the function object be a part of the source code.