Skip to content

Instantly share code, notes, and snippets.

@jfhbrook
Created January 14, 2011 04:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfhbrook/779178 to your computer and use it in GitHub Desktop.
Save jfhbrook/779178 to your computer and use it in GitHub Desktop.
(roll 3 d 6 + 2)
;As in, 2d6
;Name chosen for ease of evaluation in roll-function
(defun d (num sides)
(+ num
(loop repeat num
sum (random sides))))
(defmacro append-if-not-nil (&body body)
`(apply #'append (remove-if (lambda (x) (equal x nil)) (list ,@body))))
(defmacro remove-if-nil (&body body)
`(remove-if (lambda (x) (equal x nil)) ,@body))
(defun do-infix (infixed ops)
(labels ((applicator (clean dirty allowed-ops)
(let ((lhs (car dirty))
(op (cadr dirty))
(rhs (caddr dirty))
(still-dirty (cdddr dirty)))
(if (equal still-dirty nil)
(if (member op allowed-ops)
(append clean (list (eval (list op lhs rhs))))
(append clean (list lhs op rhs)))
(if (member op allowed-ops)
(applicator clean
(append (list (eval (list op lhs rhs)))
still-dirty)
allowed-ops)
(applicator (append clean (list lhs op))
(append (list rhs) still-dirty)
allowed-ops))))))
(remove-if-nil (applicator nil infixed ops))))
(defun roll-function (args)
;;Some sort of iterating higher-order function/macro would probably be better
(do-infix (do-infix args '(d)) '(+ -)))
(defmacro roll (&body args)
`(roll-function '(,@args)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment