I saw this example code:
(mapcar '1+ '(2 4 6))
⇒ (3 5 7)
But I do not understand '1+
here. it will apply a function to a list but this style obviously not like a function. If means add, the function should be +, what does '1+
mean?
I saw this example code:
(mapcar '1+ '(2 4 6))
⇒ (3 5 7)
But I do not understand '1+
here. it will apply a function to a list but this style obviously not like a function. If means add, the function should be +, what does '1+
mean?
It's really a function, which means add 1. you can try below example:
ELISP> (1+ 3)
4 (#o4, #x4, ?\C-d)
The implementation is at data.c:
DEFUN ("1+", Fadd1, Sadd1, 1, 1, 0,
doc: /* Return NUMBER plus one. NUMBER may be a number or a marker.
Markers are converted to integers. */)
(register Lisp_Object number)
{
CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (number);
if (FLOATP (number))
return (make_float (1.0 + XFLOAT_DATA (number)));
XSETINT (number, XINT (number) + 1);
return number;
}
'1+
is a reference to the function stored in that symbol, in this case it returns number incremented by 1. mapcar
applies it to every element of the list, and then returns the resulting list.