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
4

3 回答 3

2

为了让 CLISP 执行您的功能,您应该输入(ask-num),而不是。ask-num

[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]> 
于 2011-05-31T03:14:01.767 回答
0

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

但是,该代码在带有 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]>
于 2011-05-31T03:03:33.293 回答
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

于 2011-07-03T20:46:43.177 回答