1

I'm trying to teach myself some LISP and while I understand most of it, I have trouble grasping the eval function. I know that it does it for us already and that it's not good to use (so I hear), but how would I make a function that just adds?

So far I was attempting/thinking

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

Not really sure where to go on this, any help is appreciated.

4

1 回答 1

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
于 2013-04-15T16:28:38.680 回答