Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
    
(9+ 2) or (2+ 2) does't work, no such function defined at all. – beetlej 5 hours ago
    
Emacs can confirm for you that 1+ is the name of a function: C-h f 1+ RET – phils 2 hours ago

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;
}
share|improve this answer

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

share|improve this answer

+ has an arity of 2. It needs both (left-hand side) LHS and RHS. RHS comes from the list. Where does LHS come from? That's where 1 comes in.

You can replace it with '9+ and each element is incremented by 9.

(mapcar '9+ '(2, 4, 6)) 

Or you can replace the whole '1+ with a function that takes only one argument, such as:

(mapcar 'exp '(2, 4, 6))  

(9+ 2) or (2+ 2) does't work, no such function defined at all. –beetlej

That is because 9+ is neither a function nor a symbol at that position in the syntax. The correct syntax for the addition function is:

(+ 9 2)

or

(+ 2 2)

But '1+ worked previously because mapcar is expecting a function for the first argument and a sequence for the second argument. Both are supplied as quoted symbols.

(mapcar '1+ '(2, 4, 6))

Symbols reference is here.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.