How do I subtract 1 from the second element of the list
(setq x '(3 6)) (- (nth 1 x) 1)
The above quote does the math but does not return a modified list.
Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. It only takes a minute to sign up.Sign up to join this community
Using the built-in
cl-lib package, you can write the following:
(eval-when-compile (require 'cl-lib)) (cl-decf (nth 1 x))
Alternatively, in vanilla Elisp:
(setf (nth 1 x) (1- (nth 1 x)))
Be careful, though, to avoid modifying quoted constant lists such as
'(3 6) in-place, as doing so modifies the constant globally and may cause the code to behave differently in separate runs.
Instead, make sure to operate on a new list each time, e.g. via:
(setq x (list 3 6))