Search code examples
lispcommon-lisp

What is the difference between "apply" and "mapcar" in Lisp


(defun describe-paths (location edges) 
(apply #'append (mapcar #'describe-path (cdr (assoc location edges)))))

Solution

  • What are the similarities? Or, is there another question lurking here?

    (Links from elisp, because that is what I know. The quotes are just excerpts and the links contain examples which may or may not be relevant in a particular "Lisp".)

    mapcar

    mapcar is a function that calls its first argument with each element of its second argument, in turn. The second argument must be a sequence.

    apply (in Calling Functions)

    apply calls function with arguments, just like funcall but with one difference: the last of arguments is a list of objects, which are passed to function as separate arguments, rather than a single list. We say that apply spreads this list so that each individual element becomes an argument.

    Happy coding.