Your "illegal function call" is because you typed "(1)". You can't call 1. Probably you wanted (list 1).
Your second issue (aside from the improper call to CONS, mentioned already) is that in your CONDs you're doing some addition and doing nothing with the return values.
Code: Select all
(defun incr (x n)
(cond ((null x) (cond ((= n 1) (list 1))
(t ())))
(t (+ (car x) 1) ; <--- Here
(cond ((> (car x) 9)
(- (car x) 10) <--- Here
(cons (car x) (incr (cdr x) 1)))
(t (cons (car x) (incr (cdr x) 0)))))))
Where you use COND, it would be more normal to use IF, to avoid extra parens, the explicit (t ... ) clause, and to make it clear that there are only two branches.
Code: Select all
(defun incr2 (x n)
(if (null x)
(if (= n 1) (list 1)
'())
(progn (+ (car x) 1) ; This addition still does nothing.
(if (> (car x) 9)
(progn (- (car x) 10) ; This subtraction still does nothing.
(cons (car x) (incr (cdr x) 1)))
(cons (car x) (incr (cdr x) 0))))))
I'm not really sure what you're trying to do, but was it something like this?
Code: Select all
(defun incr3 (x) ; I have no idea what N was for.
(mapcar (lambda (y) (mod (1+ y) 10))
x))
Mapcar will call a provided function on each element of a list, and return a new list containing the result of that function for each item in the original list. You can look it up in the Hyperspec for a more complete description.
If you wanted a recursive version, it is
here. Don't peek if you wanted to do it yourself. Keep in mind that you don't have to reimplement MOD, and you especially don't need to do it inline—even if you did have to write it yourself, you should have put it in another function (named MOD, perhaps). Also keep in mind that addition does not modify anything. It just returns a value. This recursive function is not tail recursive, so it is sure to fail on long lists. A tail recursive version follows. If tail calls are optimized in your Lisp, this one will work on arbitrarily long lists.
Code: Select all
(defun incr5 (x &optional a)
(if (null x)
(nreverse a)
(incr5 (cdr x) (cons (mod (1+ (car x)) 10) a))))
My use of NULL is technically superfluous.
Code: Select all
(defun incr6 (x &optional a)
(if x
(incr5 (cdr x) (cons (mod (1+ (car x)) 10) a))
(nreverse a)))