2013-04-15 1 views
1

나는 약간의 LISP를 가르치려고하고 있는데, 대부분 이해하고 있지만, 나는 eval 함수를 이해하는 데 어려움이있다. 나는 그것이 이미 우리를 위해 그것을하고 그것을 사용하는 것이 좋지 않다는 것을 안다. (그래서 나는 듣는다.) 그러나 나는 단지 어떻게 추가 할 함수를 만들까요?LISP 동적 함수?

지금까지 나는이 가고 정말 확실하지

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

생각/시도했다, 어떤 도움에 감사드립니다.

+0

당신이 뭘 하려는지? 'eval'을 사용하지 않고 읽고있는 표현에 따라 다른 함수를 호출하고자하는 것 같습니다. 이것은 기본적으로 대부분의 Lisp 서적 (Little Schemer 등)에서 다루어지는 작은 통역사를 작성합니다. 그러나 문제를보다 명확하게 설명 할 수 있다면 도움이 될 것입니다. –

답변

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 
+0

LISP는 '두 번째'와 '세 번째'표현이 무엇인지 알아 내려고합니다. – Nogg

+0

'(두 번째 표현식)'은'expression'리스트의 두 번째 요소입니다. – Svante

+0

컴파일 된 코드가 작동하지 않는 것 같습니다. – Nogg