I am writing a function in lisp and I am not getting a result.
The function is to count the number of atoms in a expression.
(defun count-atoms(exp)
'Return the total number of non-nil atoms in the expression'
(cond((null exp) 0)
((atom exp) 1)
( t (+ (count-atoms (first exp))
(count-atoms (rest exp))))))
When I run in clisp, all I get is the following without the result.
[3]> (count-atoms '(a b c))
(COND ((NULL EXP) 0) ((ATOM EXP) 1)
(T (+ (COUNT-ATOMS (FIRST EXP)) (COUNT-ATOMS (REST EXP)))))
Can anyone help?
I’m surprised that you get a result at all. I get an error:
The reason is that strings in Common Lisp must be in double quotes:
"Return ...". Single quotes are only used to prevent evaluation.