2011-05-31 42 views
0

我在我的fedora-13機器上安裝了clisp。在CLISP解釋,我已經進入了以下內容:common lisp - ch 02,代碼錯誤?

(defun ask-num() 
    (format t "Please enter a number.") 
    (let ((val (read))) 
     (if (numberp val) 
      val 
      (ask-num)))) 

下面是保羅·格雷厄姆的書的原代碼:

(defun ask-number() 
    (format t "Please enter a number. ") 
    (let ((val (read))) 
    (if (numberp val) 
     val 
     (ask-number)))) 

是有什麼我已經錯過了?這看起來更像是解釋者的特質,而不是代碼中的錯誤。 Here是鏈接。您可能需要按Ctrl-F查看相關代碼。

更新:哈哈,對...問題!

[9]> (defun ask-num() 
    (format t "Please enter a number.") 
    (let ((val (read))) 
     (if (numberp val) 
      val 
      (ask-num)))) 
ASK-NUM 
[10]> ask-num 

*** - SYSTEM::READ-EVAL-PRINT: variable ASK-NUM has no value 
The following restarts are available: 
USE-VALUE  :R1  Input a value to be used instead of ASK-NUM. 
STORE-VALUE :R2  Input a new value for ASK-NUM. 
ABORT   :R3  Abort main loop 
+1

有你錯過了什麼?是的,這是你遇到的真正的_problem_ :-)這段代碼在我的Ubuntu clisp上工作得很好。 – paxdiablo 2011-05-31 02:57:03

回答

2

你應該輸入(ask-num),不ask-num,纔能有CLISP執行的功能。

[1]> (defun ask-num() 
    (format t "Please enter a number.") 
    (let ((val (read))) 
     (if (numberp val) 
      val 
      (ask-num)))) 
ASK-NUM 
[2]> (ask-num) 
Please enter a number.1 
1 
[3]> ask-num 

*** - SYSTEM::READ-EVAL-PRINT: variable ASK-NUM has no value 
The following restarts are available: 
USE-VALUE  :R1  Input a value to be used instead of ASK-NUM. 
STORE-VALUE :R2  Input a new value for ASK-NUM. 
ABORT   :R3  Abort main loop 
Break 1 [4]> 
+0

輝煌謝謝 – Ramy 2011-05-31 03:28:35

0

由於您沒有指出您遇到的問題,我所能做的最好的就是嘗試並複製它。

然而,該代碼Ubuntu的10下工作得很好,與CLISP 2.44.1:

[email protected]:~$ clisp 

    i i i i i i i  ooooo o  ooooooo ooooo ooooo 
    I I I I I I I  8  8 8   8  8  o 8 8 
    I \ `+'/I  8   8   8  8  8 8 
    \ `-+-'/  8   8   8  ooooo 8oooo 
    `-__|__-'  8   8   8   8 8 
     |   8  o 8   8  o  8 8 
    ------+------  ooooo 8oooooo ooo8ooo ooooo 8 

Welcome to GNU CLISP 2.44.1 (2008-02-23) <http://clisp.cons.org/> 

Copyright (c) Bruno Haible, Michael Stoll 1992, 1993 
Copyright (c) Bruno Haible, Marcus Daniels 1994-1997 
Copyright (c) Bruno Haible, Pierpaolo Bernardi, Sam Steingold 1998 
Copyright (c) Bruno Haible, Sam Steingold 1999-2000 
Copyright (c) Sam Steingold, Bruno Haible 2001-2008 

Type :h and hit Enter for context help. 

[1]> (defun ask-num() 
    (format t "Please enter a number.") 
    (let ((val (read))) 
     (if (numberp val) 
      val 
      (ask-num)))) 
ASK-NUM 
[2]> (ask-num) 
Please enter a number.hello 
Please enter a number.goodbye 
Please enter a number.3141592653589 
3141592653589 
[3]> 

因此,所有我真的可以建議是,你嘗試做正是什麼上述談話節目。如果仍然存在問題,請確保您有最新版本的CLISP,並更新問題與您遇到的實際問題(所有良好的問題應該預期實際行爲以及導致問題的情況) 。


現在你已經發布你的實際的錯誤,我們可以看到它是如何被稱爲擺在首位的功能簡單的事。你有(ask-num)調用它,在你給的鏈接規定:

(ask-number) 
Please enter a number. a 
Please enter a number. (ho hum) 
Please enter a number. 52 
52 

爲什麼你得到錯誤的原因是因爲縵ask-num正在評估作爲一個變量(當在現實中,它是一個函數)。您可以在這裏看到這些內容起作用:

[email protected]:~$ clisp 
: : : : : 
Type :h and hit Enter for context help. 

[1]> 42 
42 
[2]> myvar 

*** - EVAL: variable MYVAR has no value 
The following restarts are available: 
USE-VALUE  :R1  You may input a value to be used instead of MYVAR. 
STORE-VALUE :R2  You may input a new value for MYVAR. 
ABORT   :R3  Abort main loop 
Break 1 [3]> (set 'myvar 42) 
42 
Break 1 [3]> myvar 
42 
Break 1 [3]> 
0

正如paxdiablo所說,clisp試圖將ask-num作爲變量進行評估。 Common Lisp爲函數和其他類型的值提供了單獨的名稱空間。你可能期望一個函數被視爲一個恰好是閉包的值,但是它們被存儲並單獨查找。

如果你確實想要關閉,您可以輸入

[1]> #'ASK-NUM 

返回

#<FUNCTION ASK-NUM NIL (DECLARE (SYSTEM::IN-DEFUN ASK-NUM)) 
    (BLOCK ASK-NUM (FORMAT T "Please enter a number.") 
    (LET ((VAL (READ))) (IF (NUMBERP VAL) VAL (ASK-NUM))))> 

你可以閱讀更多關於它在這裏:http://en.wikipedia.org/wiki/Common_Lisp#The_function_namespace