6

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?

Drew
  • 75,699
  • 9
  • 109
  • 225
beetlej
  • 1,056
  • 1
  • 7
  • 20
  • (9+ 2) or (2+ 2) does't work, no such function defined at all. – beetlej Feb 17 '17 at 20:25
  • 3
    Emacs can confirm for you that `1+` is the name of a function: `C-h f 1+ RET` – phils Feb 17 '17 at 23:22
  • 1
    Tangentially there is also a `1-` function which returns its argument minus 1. That one is also slightly less intuitive when looking at the code: `(1- 3) => 2`. – phils Feb 18 '17 at 04:02

2 Answers2

8

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;
}
lucky1928
  • 1,622
  • 8
  • 28
1

'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.