常见的lisp - ch 02,代码错误?

时间:2011-05-31 02:49:42

标签: clisp

我在fedora-13机器上安装了clisp。在clisp解释器中,我输入了以下内容:

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

以下是Paul Graham的书中的原始代码:

(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

3 个答案:

答案 0 :(得分: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]> 

答案 1 :(得分:0)

既然你没有表明你遇到的问题,我能做的最好就是尝试复制它。

但是,在Ubuntu 10下使用CLISP 2.44.1:

,该代码可以正常工作
pax@pax-desktop:~$ 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被评估为变量(实际上,它是一个函数)。你可以在这里看到这个:

pax@pax-desktop:~$ 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]>

答案 2 :(得分: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