practice/lisp/land-of-lisp/ch2/guess-my-number-game.lisp

24 lines
611 B
Common Lisp
Raw Normal View History

2017-06-23 02:16:42 -07:00
(defparameter *small* 1)
(defparameter *big* 100)
(defun guess-my-number ()
;; ash is the artithmetic shift function
;; Halvs the sum of the limits and shortens the result
;; ash, here, shifts the binary bits left or right (well, right in this case)
;; Which leads to the right most binary falling off
;; This is a binary search
(ash (+ *small* *big*) -1))
(defun smaller ()
(setf *big* (1- (guess-my-number)))
(guess-my-number))
(defun bigger ()
(setf *small* (1+ (guess-my-number)))
(guess-my-number))
(defun start-over ()
(defparameter *small* 1)
(defparameter *big* 100)
(guess-my-number))