【问题标题】:Read Statement Being Skipped Over, Unbound Variable读取语句被跳过,未绑定变量
【发布时间】:2010-10-27 08:49:28
【问题描述】:

我仍在用 Common Lisp 开发我的猜数字游戏,但我已经陷入停顿状态。当调用以下代码时:

;;;; number-game.lisp
;;;;
;;;; Andrew Levenson
;;;; 10/25/2010
;;;;
;;;; Simple number guessing game. User has
;;;; five guesses to determine a number between
;;;; one and one hundred, inclusive (1-100).

;;; Set global variable for the target number:
(defparameter *target* nil) 

;;; Set the iterator so we may check the number of guesses
(defparameter *number-of-guesses* 0)

;;; Welcome the user
(defun welcome-user ()
 (format t "Welcome to the number guessing game!~%"))

;;; Prompt for a guess
(defun prompt-for-guess ()
 (format t "Please enter your guess (1-100): ")
 (finish-output nil) ; nil directs finish-output to standard IO
 (check-guess 'read-guess))

;;; Read in a guess
(defun read-guess ()
 (let (guess (read)))
 (if (numberp guess) ; If true, return guess. Else, call prompt-for-guess
  (progn
   (setq *number-of-guesses* (+ *number-of-guesses* 1))
   guess)
  (prompt-for-guess)))

;;; Check if the guess is higher than, lower than, or equal to, the target
(defun check-guess (fn)
 (setq guess (funcall fn))
 (if (equal guess *target*)
  (equal-to)
  (if (> guess *target*)
   (greater-than (guess))
   (if (< guess *target*)
    (less-than (guess))))))

;;; If the guess is equal to the target, the game is over
(defun equal-to ()
 (format t "Congratulations! You have guessed the target number, ~a!~%" *target*)
 (y-or-n-p "Play again? [y/n] "))

;;; If the guess is greater than the target, inform the player.
(defun greater-than (guess)
 (format t "Sorry, ~a is greater than the target.~%" guess)
 (if (< *number-of-guesses* 6)
  (prompt-for-guess)
  (game-over)))

;;; If the guess is less than the target, inform the player.
(defun less-than (guess)
 (format t "Sorry, ~a is less than the target.~%" guess)
 (if (< *number-of-guesses* 6)
  (prompt-for-guess)
  (game-over)))

;;; If the player has run out of guesses, give them the option
;;; of playing the game again.
(defun game-over ()
 (y-or-n-p "You have run out of guesses. Play again? [y/n] "))


;;; Play the game
(defun play ()
 ;; If it's their first time playing this session,
 ;; make sure to greet the user.
 (unless (> *number-of-guesses* 0)
  (welcome-user))
 ;; Reset their remaining guesses
 (setq *number-of-guesses* 0)
 ;; Set the target value
 (setq *target*
  ;; Random can return float values,
  ;; so we must round the result to get
  ;; an integer value.
  (round
   ;; Add one to the result, because
   ;; (random 100) yields a number between
   ;; 0 and 99, whereas we want a number
   ;; from 1 to 100 inclusive.
   (+ (random 100) 1)))
 (if (equal (prompt-for-guess) "y")
  (play)
  (quit)))

我得到了错误

* (play)
Welcome to the number guessing game!
Please enter your guess (1-100): 
debugger invoked on a UNBOUND-VARIABLE in thread #<THREAD
                                                   "initial thread" RUNNING
                                                   {AA14959}>:
  The variable GUESS is unbound.

Type HELP for debugger help, or (SB-EXT:QUIT) to exit from SBCL.

restarts (invokable by number or by possibly-abbreviated name):
  0: [ABORT] Exit debugger, returning to top level.

(READ-GUESS)

为什么会跳过read-guess 中的读取语句?我很确定该变量是未绑定的,因为它不允许我输入任何内容。

【问题讨论】:

    标签: lisp common-lisp sbcl


    【解决方案1】:

    检查 LET 的语法。

    例子:

    (let ((variable (compute-some-value)))
       (do-something-with variable)
       (do-some-more))
    

    【讨论】:

      猜你喜欢
      • 2021-06-29
      • 2023-01-30
      • 2017-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-04
      相关资源
      最近更新 更多