LISP动态函数?

时间:2013-04-15 16:11:33

标签: lisp common-lisp

我正在尝试自学一些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)))

不确定该去哪里,感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

(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