Created
January 25, 2011 23:08
-
-
Save llibra/795894 to your computer and use it in GitHub Desktop.
This file contains 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
(defun square (x) (expt x 2)) | |
;; => SQUARE | |
(define-compiler-macro square (&whole form arg) | |
(if (atom arg) | |
`(expt ,arg 2) | |
(case (car arg) | |
(square (if (= (length arg) 2) | |
`(expt ,(nth 1 arg) 4) | |
form)) | |
(expt (if (= (length arg) 3) | |
(if (numberp (nth 2 arg)) | |
`(expt ,(nth 1 arg) ,(* 2 (nth 2 arg))) | |
`(expt ,(nth 1 arg) (* 2 ,(nth 2 arg)))) | |
form)) | |
(otherwise `(expt ,arg 2))))) | |
;; => SQUARE | |
(square (square 3)) | |
;; => 81 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment