Lisp 正在跳过的Read语句,未绑定变量
我还在用CommonLisp做我的数字猜谜游戏,我已经停了下来。调用以下代码时:Lisp 正在跳过的Read语句,未绑定变量,lisp,common-lisp,sbcl,Lisp,Common Lisp,Sbcl,我还在用CommonLisp做我的数字猜谜游戏,我已经停了下来。调用以下代码时: ;;;; 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). ;;;
;;;; 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)))
;;;;number-game.lisp
;;;;
;;;; 安德鲁·莱文森
;;;; 10/25/2010
;;;;
;;;; 简单的数字猜谜游戏。用户拥有
;;;; 五次猜测以确定
;;;; 壹佰壹拾贰元(含壹至壹佰元)。
;;; 为目标编号设置全局变量:
(defparameter*目标*nil)
;;; 设置迭代器,以便检查猜测的次数
(defparameter*猜测次数*0)
;;; 欢迎用户
(defun欢迎用户()
(格式t“欢迎参加猜数字游戏!~%”)
;;; 提示猜测
(取消提示猜测()
(格式t“请输入您的猜测(1-100):”)
(完成输出nil);nil将完成输出定向到标准IO
(勾选“阅读猜测”)
;;; 猜一猜
(取消读取猜测()
(让(猜(读)))
(if(numberp guess);如果为true,则返回guess。否则,调用prompt进行猜测
(项目
(setq*猜测次数*(+*猜测次数*1))
(猜测)
(提示猜测)
;;; 检查猜测值是否高于、低于或等于目标值
(取消检查猜测(fn)
(setq猜测(funcall fn))
(如果(相等猜测*目标*)
(等于)
(如果(>猜测*目标*)
(大于(猜测))
(如果(*猜测次数*0)
(欢迎用户)
重置他们剩余的猜测
(setq*猜测次数*0)
设置目标值
(setq*目标*
;Random可以返回浮点值,
所以我们必须把结果四舍五入才能得到结果
;整数值。
(圆形
在结果中添加一个,因为
;;;(随机100)产生一个介于
;0和99,而我们想要一个数字
;从1到100,包括在内。
(+(随机100)1)))
(如果(等于(提示猜测)“y”)
(播放)
(退出)
我得到了错误
* (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)
*(播放)
欢迎来到数字猜谜游戏!
请输入您的猜测(1-100):
在线程#中的未绑定变量上调用调试器:
变量GUESS是未绑定的。
键入“帮助”以获取调试器帮助,或键入(SB-EXT:QUIT)以退出SBCL。
重新启动(可通过数字或可能的缩写名称调用):
0:[中止]退出调试器,返回顶层。
(阅读猜测)
为什么它会跳过
read guess
中的read语句?我很确定这个变量是未绑定的,因为它不允许我输入任何内容。检查LET的语法
例如:
(let ((variable (compute-some-value)))
(do-something-with variable)
(do-some-more))