LISP mapcar add 1 to each integer in a list

128 Views Asked by At
(defun add-1-all (list)
   (mapcar #'1+ '(list))
  )

I am trying to pass a list of integers and add 1 to each integer in the list with mapcar, but I keep getting list is defined but never used.

; in: DEFUN ADD-1-ALL
;     (DEFUN ADD-1-ALL (LIST) (MAPCAR #'1+ '(LIST)))
; --> SB-IMPL::%DEFUN SB-IMPL::%DEFUN SB-INT:NAMED-LAMBDA 
; ==>
;   #'(SB-INT:NAMED-LAMBDA ADD-1-ALL
;         (LIST)
;       (BLOCK ADD-1-ALL (MAPCAR #'1+ '(LIST))))
; 
; caught STYLE-WARNING:
;   The variable LIST is defined but never used.
; 
; compilation unit finished
;   caught 1 STYLE-WARNING condition
2

There are 2 best solutions below

0
Barmar On

Since you're quoting (list), this is a list containing the literal symbol list, it not the value of the variable. That's why you get a warning about an unused variable.

And when you run it, you'll get an error because 1+ requires its argument to be a number, not the symbol list.

The second argument to mapcar should be the value of the list itself, so just used the variable directly.

(defun add-1-all (list)
   (mapcar #'1+ list)
)
0
Rainer Joswig On

Lisp can be interactively used.

Just type '(list) to the read eval print loop:

  * '(list)
  (LIST)

  * (describe '(list)) 
  (LIST)
    [list]

So you see that '(list) gets evaluated to a list (LIST).

You have written the list as literal data.

A variable is in Lisp an unquoted symbol, here just list.