I want to write a small function to add a value to a list. it looks like this:
(defvar fares '(31.14 28.12 25.10 22.08 19.06 16.04 13.02 10))
(defun plus-extra (fare) (+ 3.02 fare))
(map 'plus-extra fares)
Fairly predictably, the elisp barfs because the function needs an argument. What am I missing?
Thanks Robert
Calling a function is also known as invocation. The most common way of invoking a function is by evaluating a list. For example, evaluating the list (concat "a" "b") calls the function concat with arguments "a" and "b" . See Evaluation, for a description of evaluation.
Emacs Lisp is a dialect of the Lisp programming language used as a scripting language by Emacs (a text editor family most commonly associated with GNU Emacs and XEmacs). It is used for implementing most of the editing functionality built into Emacs, the remainder being written in C, as is the Lisp interpreter.
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. The ' map ' part of the name comes from the mathematical phrase, “mapping over a domain”, meaning to apply a function to each of the elements in a domain.
function (aka #' ) is used to quote functions, whereas quote (aka ' ) is used to quote data.
The function which doesn't have enough argument here is map
, not the one you defined.
The map
function does not exists in Emacs Lisp, it is provided by the cl
package. This map
function require 3 arguments, the first one must be the type of what map
should return. This:
(map 'list 'plus-extra fares)
will work. But what you want is this:
(mapcar 'plus-extra fares)
which is native elisp.
PS: Don't forget that Emacs comes with its documentation! C-h f map RET ;-).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With