Doesn't Matter
Doesn't Matter

Reputation: 405

list manipulation

I am a lisp novice and I am trying to manipulate lists in lisp. The is from practical tutorial in uni. When i call the function the first element in the list need to be incremented by one and the rest to remain as it was. Here is an example:

(inc-1st '(1 2 3 4))  =>  (2 2 3 4) 

I tried to solve it but my first number from the list is not printing. Here is my code:

(defun inc-1st (list)
    (and (+ 1(car list)) (cdr list)))

and the output is: (2 3 4)

Upvotes: 0

Views: 132

Answers (1)

sds
sds

Reputation: 60014

The standard CL macro INCF will do what you want:

[1]> (defparameter list (list 1 2 3))
LIST
[2]> (incf (first list))
2
[3]> list
(2 2 3)

(Try (macroexpand (incf (first list))) to see how it works.)

Thus your function would be something like

(defun inc-1st (list)
  (incf (first list))
  list)

Note the difference between printing value and returning it: the function above returns list while the REPL prints the return value.

Upvotes: 1

Related Questions