;; "Macros" are used to extend the Lisp language. They consist of a ;; function which instead of returning a computed value, transform ;; their unevaluated arguments into a new form that, when evaluated, ;; produces the actual value of the original form. (require 'stdlib) ;; for testing macro expansions (defmacro mac (form) `(puts (macroexpand-1 ',form))) (defmacro when_list (condition #!rest body) (list 'cond (list* condition body))) ;; using backquoting (defmacro when (condition #!rest body) `(cond (,condition ,@body))) (when (< 1 2) (puts "yep")) (mac (when (slarti) (bartfast))) (defmacro inc (var) `(setq ,var (1+ ,var))) (let ((i 0)) (while (< i 4) (puts i) (inc i))) ;; see section on macros in lispref (defmacro for_verbose (var from init to final do #!rest body) "Execute a simple for loop: (for i from 1 to 10 do (print i))." (let ((tempvar (make-symbol "max"))) `(let ((,var ,init) (,tempvar ,final)) (while (<= ,var ,tempvar) ,@body (inc ,var))))) (mac (for_verbose i is 1 to 10 do (princ i))) (for i 42 46 (puts (number->string i))) (defmacro for (var init final #!rest body) (let ((tempvar (make-symbol "max"))) `(let ((,var ,init) (,tempvar ,final)) (while (<= ,var ,tempvar) ,@body (inc ,var))))) (mac (for i 3 5 (fuzzle i))) (for i 42 46 (puts i)) (defmacro dotimes (var final #!rest body) (let ((tmp (make-symbol "max"))) `(let ((,var 0) (,tmp ,final)) (while (< ,var ,tmp) ,@body (inc ,var))))) (mac (dotimes i 4 (frobnicate i))) (dotimes i 4 (puts i))