How do I add a number, say 2, to every item in a list?
(setq x '(1 2)) (+ 2 x) (mapcar '2+ x) (loop for i in x do (+ 2 i)) (dolist (i x) (+ 2 i))
None of the above work.
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
That's not very elegant (there's probably a much simpler answer) but this should work:
(setq x '(1 2 3 4)) (defun add-one (x) "Add 1 to each element of list X" (setq counter 0) (setq res x) (while (< counter (length x)) (setcar (nthcdr counter res) (1+ (car (nthcdr counter x)))) (setq counter (1+ counter))) res) (add-one x)