common lisp - ch 02,代码错误?
我已经在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该输入
(ask-num)
,而不是ask-num
,以便 CLISP 执行您的函数。You should be typing
(ask-num)
, notask-num
, in order to have CLISP execute your function.由于您没有指出您遇到的问题,所以我能做的最好的事情就是尝试复制它。
然而,该代码在带有 CLISP 2.44.1 的 Ubuntu 10 下运行得很好:
所以,我真正能建议的是你尝试完全执行上面的记录显示的内容。如果仍然存在问题,请确保您拥有最新版本的 CLISP,并根据您遇到的实际问题更新问题(所有好的问题都应该具有预期的和实际行为以及导致问题的情况)直到问题为止)。
现在您已经发布了实际的错误,我们可以看到这只是您最初如何调用该函数的简单问题。您必须使用
(ask-num)
调用它,如您提供的链接中所指定:您收到该错误的原因是因为一个未经修饰的
ask-num
被评估为一个变量(实际上,它是一个函数)。您可以在这里看到它的实际效果: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:
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: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:正如 paxdiablo 所说,clisp 试图将 Ask-num 作为变量进行评估。 Common Lisp 对于函数和其他类型的值有单独的命名空间。您可能期望函数仅被视为恰好是闭包的值,但它们是单独存储和查找的。
如果您确实想要闭包,您可以输入
which returns
您可以在此处阅读更多相关信息: 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
which returns
You can read more about it here: http://en.wikipedia.org/wiki/Common_Lisp#The_function_namespace