Skip to content

Instantly share code, notes, and snippets.

@llibra
Created January 25, 2011 23:08
Show Gist options
  • Save llibra/795894 to your computer and use it in GitHub Desktop.
Save llibra/795894 to your computer and use it in GitHub Desktop.
(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