common lisp - ch 02,代码错误?

发布于 2024-11-11 15:03:30 字数 1079 浏览 7 评论 0原文

我已经在我的 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))))

有什么我错过的吗?这看起来更像是解释器的特性,而不是代码中的错误。 这里是链接。您可能必须按 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

I've installed clisp on my fedora-13 machine. In the clisp interpreter, i've entered the following:

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

Here is the original code from Paul Graham's book:

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

is there something I've missed? this seems more like an idiosyncracy with the interpreter rather than an error in the code. Here is the link. You may have to ctrl-F for the code in question.

UPDATE: haha, right...the problem!

[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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花桑 2024-11-18 15:03:30

您应该输入 (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]> 

You should be typing (ask-num), not ask-num, in order to have CLISP execute your function.

[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]> 
笑忘罢 2024-11-18 15:03:30

由于您没有指出您遇到的问题,所以我能做的最好的事情就是尝试复制它。

然而,该代码在带有 CLISP 2.44.1 的 Ubuntu 10 下运行得很好:

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]>

Since you don't indicate the problem you're having, the best I can do is to try and replicate it.

However, that code works just fine under Ubuntu 10 with 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]> 

So, all I can really suggest is that you try to do exactly what the above transcript shows. If that still has a problem, make sure you have a recent version of CLISP and update the question with the actual problem you're having (all good questions should have expected and actual behaviour along with the circumstances leading up to the problem).


Now that you've posted your actual error, we can see it's a simple matter of how you called the function in the first place. You have to call it with (ask-num), as specified in the link you gave:

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

The reason why you're getting that error is because an unadorned ask-num is being evaluated as a variable (when, in reality, it's a function). You can see this in action here:

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]>
↙厌世 2024-11-18 15:03:30

正如 paxdiablo 所说,clisp 试图将 Ask-num 作为变量进行评估。 Common Lisp 对于函数和其他类型的值有单独的命名空间。您可能期望函数仅被视为恰好是闭包的值,但它们是单独存储和查找的。

如果您确实想要闭包,您可以输入

[1]> #'ASK-NUM

which returns

#<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

As paxdiablo said, clisp was trying to evaluate ask-num as a variable. Common Lisp has separate namespaces for functions and other kinds of values. You might expect a function to just be treated as a a value which happens to be a closure, but instead they're stored and looked up separately.

If you did want the closure, you could enter

[1]> #'ASK-NUM

which returns

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

You can read more about it here: http://en.wikipedia.org/wiki/Common_Lisp#The_function_namespace

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文