Skip to content

Instantly share code, notes, and snippets.

@schmalz
Created July 25, 2022 11:02
Show Gist options
  • Save schmalz/738ba320f824a275db0b3f560e1a0b7f to your computer and use it in GitHub Desktop.
Save schmalz/738ba320f824a275db0b3f560e1a0b7f to your computer and use it in GitHub Desktop.
(defun throw-die ()
"Simulate the throw of a regular die; returns a number between 1 and 6, inclusive."
(1+ (random 6)))
(defun throw-dice ()
"Simulate the throwing of two dice; returns a list."
(list (throw-die) (throw-die)))
(defun snake-eyes-p (throw)
"Is THROW snake-eyes (two ones)?"
(equal (apply #'+ throw) 2))
(defun boxcars-p (throw)
"Is THROW boxcars (two sixes)?"
(equal (apply #'+ throw) 12))
(defun instant-win-p (throw)
"Is THROW an instant win?"
(let ((pips (apply #'+ throw)))
(or (equal pips 7)
(equal pips 11))))
(defun instant-loss-p (throw)
"Is THROW an instant loss?"
(let ((pips (apply #'+ throw)))
(or (equal pips 2)
(equal pips 3)
(equal pips 12))))
(defun say-throw (throw)
"A representation of THROW."
(cond ((snake-eyes-p throw) 'snake-eyes)
((boxcars-p throw) 'boxcars)
(t (apply #'+ throw))))
(defun describe-throw (throw win-or-lose)
"A description of THROW, either a win or a lose."
`(throw ,(first throw) and ,(second throw) -- ,(say-throw throw) -- you ,win-or-lose))
(defun describe-throw-again (throw)
"A description of THROW, again."
`(throw ,(first throw) and ,(second throw) -- ,(say-throw throw) -- throw again))
(defun describe-point (throw)
"A description of THROW, a point."
`(throw ,(first throw) and ,(second throw) -- your point is ,(say-throw throw)))
(defun craps ()
"Simulate the first throw in a game of Craps."
(let ((throw (throw-dice)))
(cond ((instant-win-p throw) (describe-throw throw 'win))
((instant-loss-p throw) (describe-throw throw 'lose))
(t (describe-point throw)))))
(defun try-for-point (point)
"Simulate trying for point in a game of Craps."
(let ((throw (throw-dice)))
(cond ((equal (apply #'+ throw) point) (describe-throw throw 'win))
((instant-loss-p throw) (describe-throw throw 'lose))
(t (describe-throw-again throw)))))
@schmalz
Copy link
Author

schmalz commented Jul 25, 2022

I followed along with the "Craps" keyboard exercise in Common List: A Gentle Introduction to Symbolic Computing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment