当前位置: 动力学知识库 > 问答 > 编程问答 >

Why is my lisp program not running?

问题描述:

"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)

(cond

(

(null L)

(0)

)

(

(listp (car L) )

(+ (add-all (car L)) (add-all (cdr L)) )

)

(

(stringp (car L) )

(+ (add-all (cdr L)) )

)

(

t

(+ (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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: