Search code examples
clisp

common lisp - ch 02, code error?


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

Solution

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