gpt4 book ai didi

LISP 动态函数?

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

我正在尝试自学一些 LISP,虽然我了解其中的大部分内容,但我很难掌握 eval 函数。我知道它已经为我们做到了,而且它不好用(我听说),但我如何制作一个只添加的函数?

到目前为止我正在尝试/思考

(setf input-prompt "Enter addition epression: ")
(setf output-prompt "The value is: ")

(defun prompt-for-input (msg)
(format t msg))


(defun sum (expression)
(format t "Summing ~d and ~d.~%" x y)
(+ x y))


(defun add ()
(prompt-for-input input-prompt)
(let ((expression (read)))
((sum (expression)))
(add)))

不太确定该从哪里开始,感谢任何帮助。

最佳答案

(setf input-prompt "Enter addition expression: ")
(setf output-prompt "The value is: ")

(defun prompt-for-input (msg)
(format t msg)
(finish-output))

(defun sum (expression)
(let ((x (second expression))
(y (third expression)))
(format t "~%Summing ~d and ~d.~%" x y)
(+ x y)))

(defun add ()
(prompt-for-input input-prompt)
(sum (read)))

运行它:

CL-USER > (add)
Enter addition expression: (+ 1 2)
Summing 1 and 2.
3

关于LISP 动态函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16019891/

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