exercises and notes from "Gentle introduction"
This commit is contained in:
94
vars-and-effects.lisp
Normal file
94
vars-and-effects.lisp
Normal file
@@ -0,0 +1,94 @@
|
||||
;; setq defvar defun - examples of sideffectful things
|
||||
|
||||
(random 100)
|
||||
|
||||
;; setq could even change local variables (defined by function arguments)
|
||||
(defun bad-style (p)
|
||||
"This function will overdefine it's input P."
|
||||
(setf p (+ p 2)) ; changes local variable
|
||||
(setf r (* p 2))
|
||||
`(result is ,p))
|
||||
|
||||
p ; this is not set by the setf
|
||||
r ; this global value is changed
|
||||
|
||||
(bad-style 15)
|
||||
|
||||
;; wait, there's setf, what's different in setq?
|
||||
;; setf has "place" as first item, so setter forms
|
||||
|
||||
(documentation `setf `function)
|
||||
(documentation `setq `function)
|
||||
|
||||
(apropos "random")
|
||||
|
||||
;;; exercises
|
||||
|
||||
(defun throw-die ()
|
||||
(+ 1 (random 6)))
|
||||
(throw-die)
|
||||
|
||||
(defun throw-dice ()
|
||||
(list (throw-die) (throw-die)))
|
||||
(throw-dice)
|
||||
|
||||
(defun snake-eyes-p (thro)
|
||||
(equal thro `(1 1)))
|
||||
(snake-eyes-p `(1 1))
|
||||
(snake-eyes-p `(1 2))
|
||||
|
||||
(defun boxcars-p (thro)
|
||||
(equal thro `(6 6)))
|
||||
(boxcars-p `(6 6))
|
||||
(boxcars-p `(1 2))
|
||||
|
||||
(funcall `+ `(1 2)) ; 149
|
||||
(apply `+ `(1 2 5))
|
||||
|
||||
(defun instant-win-p (the-throw)
|
||||
(let ((win-hands `(7 11))
|
||||
(sum (apply #'+ the-throw)))
|
||||
(member sum win-hands)))
|
||||
|
||||
(defun instant-loss-p (the-throw)
|
||||
(let ((loose-hands `(2 3 12))
|
||||
(sum (apply #'+ the-throw)))
|
||||
(member sum loose-hands)))
|
||||
;;; breaking my head, looking for #'contains function for list
|
||||
;;; apropos doesn't quite help
|
||||
(member 7 (list 1 4 7 11)) ; well, member returns "rest"
|
||||
|
||||
(instant-win-p `(6 5)) ; so, this is correct. cool
|
||||
(or `t nil)
|
||||
|
||||
(instant-loss-p `(6 5))
|
||||
|
||||
(defun say-throw (the-throw)
|
||||
(cond ((equal the-throw `(1 1)) `snake-eyes)
|
||||
((equal the-throw `(6 6)) `boxcars)
|
||||
(t (apply #'+ the-throw))))
|
||||
(say-throw '(1 1))
|
||||
(say-throw '(6 6))
|
||||
(say-throw '(2 5))
|
||||
|
||||
;; firsth throw either insta-win, insta-loose, or set point
|
||||
(defun craps ()
|
||||
(let* ((the-throw (throw-dice ))
|
||||
(result (cond ((instant-loss-p the-throw) `(,(say-throw the-throw) -- you loose))
|
||||
((instant-win-p the-throw) `(,(say-throw the-throw) -- you win))
|
||||
(t `(your point is ,(say-throw the-throw))))))
|
||||
(append `(throw ,(first the-throw) and ,(second the-throw) -- ) result)))
|
||||
|
||||
(craps)
|
||||
|
||||
(defun try-for-point (point)
|
||||
(let* ((the-throw (throw-dice))
|
||||
(value (apply #'+ the-throw))
|
||||
(result (cond ((equal value point) `(you win))
|
||||
((equal value 7) `(you loose))
|
||||
(t `(throw again)))))
|
||||
(append `(throw ,(first the-throw) and ,(second the-throw) -- ,value --) result)))
|
||||
|
||||
(try-for-point 8)
|
||||
|
||||
;; next onto the 6, list data structures
|
||||
Reference in New Issue
Block a user