Created
January 14, 2011 04:35
-
-
Save jfhbrook/779178 to your computer and use it in GitHub Desktop.
(roll 3 d 6 + 2)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;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