lisp-猜数字算法与全局函数、变量

阅读更多
* (defvar *big* 100)

*BIG*
* (defvar *small* 1)
*SMALL*

 

50
0] (defun smaller() (setf *big* (1- (guess-number)))(guess-number))

SMALLER
0] (defun bigger() (setf *small* (1+ (guess-number)))(guess-number))

* (defun guess-number() (ash (+ *small* *big*) -1))
STYLE-WARNING: redefining COMMON-LISP-USER::GUESS-NUMBER in DEFUN

GUESS-NUMBER
* guess-number

0[2] (smaller)
; No debug variables for current frame: using EVAL instead of EVAL-IN-FRAME.
25
0[2] (bigger)
; No debug variables for current frame: using EVAL instead of EVAL-IN-FRAME.
37
0[2] (bigger)
; No debug variables for current frame: using EVAL instead of EVAL-IN-FRAME.
43
0[2] (smaller)
; No debug variables for current frame: using EVAL instead of EVAL-IN-FRAME.
40
0[2] (smaller)
; No debug variables for current frame: using EVAL instead of EVAL-IN-FRAME.
38

 

我们设定数字是38

 

 

defvar定义全局变量,defparameter也是,但是defparameter语句可以反复使用,多次定义一个变量或改变量的值

比如

(defparameter *xx* 100)

然后再执行一次

(defparameter *xx* 101)

那么此时

*xx*是101

defvar则不同

(defvar *xx* 100)

然后再执行一次

(defvar *xx* 101)

那么此时

*xx*是100

你可能感兴趣的:(lisp-猜数字算法与全局函数、变量)