gpt4 book ai didi

syntax-error - 汽车 : + is not a list, lisp 错误?

转载 作者:太空宇宙 更新时间:2023-11-03 18:47:28 28 4
gpt4 key购买 nike

我正在尝试编写一个采用多项式并对其进行化简的程序。每次我运行该程序时,通过调用“(evalexp '( (x 2) (z 8) ) p1)”我都会收到错误消息“Car: + is not a list”。它应该返回“(+ 2 (* 2 (- y 4)))” 这是我的代码:

(setq p1 '(+ x (* x (- y (/ z 2)))))
(setq p2 '(+ (- z 2) (* x 5)))
(setq p3 '(+ 1 a))
(setq p4 '(+ x (+ x (* x 0))))

(defun addexp (e1 e2) (list '+ e1 e2))
(defun subexp (e1 e2) (list '- e1 e2))
(defun mulexp (e1 e2) (list '* e1 e2))
(defun divexp (e1 e2) (list '/ e1 e2))

(defun deep-subst (old new expr)
(cond
((null expr)
nil
)
((listp (car expr))
(cons (deep-subst old new (car expr)) (deep-subst old new (cdr expr)))
)
((eq old (car expr))
(cons new (deep-subst old new (cdr expr)))
)
(T
(cons (car expr) (deep-subst old new (cdr expr)))
)
)
)

(defun subst-bindings (expr bindinglist)
(cond
((null bindinglist)
expr )
(T
(deep-subst (car (car bindinglist)) (car (cdr (car bindinglist)))
(subst-bindings expr (cdr bindinglist))
))))

(defun simplify-triple(op left-arg right-arg)
(cond
((and (numberp left-arg) (numberp right-arg))
(eval (list op left-arg right-arg))
)
((and (eq op '+) (eql right-arg 0))
left-arg
)
((and (eq op '+) (eql left-arg 0))
right-arg
)
((and (eq op '-) (eql right-arg 0))
left-arg
)
((and (eq op '-) (eql right-arg left-arg))
0
)
((and (eq op '*) (eql right-arg 0))
0
)
((and (eq op '*) (eql left-arg 0))
0
)
((and (eq op '*) (eql right-arg 1))
left-arg
)
((and (eq op '*) (eql left-arg 1))
right-arg
)
((and (eq op '/) (eql left-arg 0))
0
)
((and (eq op '/) (eql right-arg 1))
left-arg
)
((and (eq op '/) (eql right-arg left-arg))
1
)

(T
(list op left-arg (simplify right-arg))
;(list op right-arg (simplify left-arg))
)
)
)

(defun simplify (exp)
(cond
( (listp exp)
(simplify-triple (car exp) (simplify (car (cdr exp))) (simplify (car (cdr (cdr exp)))))
)
(T
exp)))


(defun evalexp (exp binding-list)
(simplify (subst-bindings exp binding-list))
)

最佳答案

找到你的错误。您的大多数函数都被声明为接收表达式作为第一个参数,绑定(bind)作为第二个参数,但是在您的示例中,您调用的函数将绑定(bind)作为第一个参数传递,将表达式作为第二个参数传递。您必须调用 (evalexp p1 '((x 2) (z 8))) 或交换函数定义的参数顺序(我建议使用此解决方案,因为它看起来更自然且看起来就像@rainer-joswig 链接的作业一样)。

特别是,subst-bindingsevalexp 是需要更改的。

关于syntax-error - 汽车 : + is not a list, lisp 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26222642/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com