"Learning" Lisp for school but don't feel I'm learning correctly. I'm trying to write a function to add all the numbers in a list that is composed of lists, numbers, and strings. I'm ignoring the strings and entering the lists. I'm getting quite lost with all the parenthesis...
Im receiving the error Illegal argument in functor position 0
(defun add-all (L)
(listp (car L) )
(+ (add-all (car L)) (add-all (cdr L)) )
(stringp (car L) )
(+ (add-all (cdr L)) )
(+ (car L) (add-all (cdr L)) )
You are attempting to call the function 0 in the first clause of your cond. ie.
(0). This is the fixed code formatted for readability.
(defun add-all (list) (cond ((null list) 0) ((listp (car list)) (+ (add-all (car list)) (add-all (cdr list)))) ((stringp (car list)) (+ (add-all (cdr list)))) (t (+ (car list) (add-all (cdr list))))))
May I ask why are you handling strings in the list?
As an extra, I recommend you use a text editor that can format the code and balance the parens for you. Emacs is the best choice. Also this code is more 'scheme'y than lispy. What book are you using to learn lisp if I may ask.
In the first clause of
cond you have:
( (null L) (0) )
I think you want:
( (null L) 0 )
... without the parens.