Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Affect one element of a list

Tags:

emacs

elisp

If I have a list of numbers

(setq numbers '(10 11 12))

and I want to increment, say, the third number, I can do this:

(setf (nth 2 numbers) (1+ (nth 2 numbers)))

But I don't like having to repeat the "(nth 2 numbers)". Is there some way I can write this but only have one reference to "(nth 2 numbers)"?

like image 482
Peter Reavy Avatar asked Feb 17 '23 00:02

Peter Reavy


1 Answers

There's a macro for exactly that:

(incf (nth 2 numbers))

You can supply the value to add as an additional argument.

like image 100
phils Avatar answered Feb 27 '23 06:02

phils