Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass arguments to map in emacs lisp?

Tags:

emacs

lisp

elisp

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

like image 915
robertpostill Avatar asked May 21 '11 13:05

robertpostill


People also ask

How do you call a function in a Common Lisp?

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.

Is Emacs Lisp the same as Lisp?

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.

What is 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. 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.

What does #' mean in Emacs Lisp?

function (aka #' ) is used to quote functions, whereas quote (aka ' ) is used to quote data.


1 Answers

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 ;-).

like image 139
p4bl0 Avatar answered Oct 28 '22 20:10

p4bl0