(cdr(l))
invokes the function l
and applies cdr
to the value it returns.
You should write (cdr l)
instead:
(t (sum (cdr l))))
문제
I'm having problems with clisp.I'm tryng just to get the sum of the numbers in a list,but it gives me this error:
EVAL-the function L is undefined
when i call
(sum '(2 (c 6)))
Here is the code:
(defun sum(l)
(cond
((null l) 0)
((NUMBERP (car l)) (+ (car l) (sum(cdr l)) ))
(t (sum(cdr(l))))
)
)
해결책
(cdr(l))
invokes the function l
and applies cdr
to the value it returns.
You should write (cdr l)
instead:
(t (sum (cdr l))))
다른 팁
(defun sum (x) (if (null x) 0
(if (numberp x) x
(+ (car x) (sum (cdr x))))))
by the way you could do similarly with product
(defun prod (x) (if (null x) 1
(if (numberp x) x
(* (car x) (prod (cdr x))))))
(eq (prod '(1 2 3 4 5 6 7 8 9 10)) (fact 10))
but define fact first - here you are a defun for fact (the usual one...)
(defun fact (n)
(if (not (integerp n)) (print "give me an integer" )
(if (< n 0) (print "give me a pos num " )
(if (= n 0) 1
(if (= n 1) 1
(* n (fact (- n 1)
)))))))