diff options
Diffstat (limited to 'macros.lisp')
-rw-r--r-- | macros.lisp | 31 |
1 files changed, 20 insertions, 11 deletions
diff --git a/macros.lisp b/macros.lisp index cc8a54c..c354a89 100644 --- a/macros.lisp +++ b/macros.lisp @@ -2,7 +2,8 @@ (eval-when (:compile-toplevel :load-toplevel :execute) (defun is-substitute-var (symbol) - (and (symbolp symbol) + (and (not (eql '$$ symbol)) + (symbolp symbol) (eq (elt (symbol-name symbol) 0) #\$)))) @@ -10,8 +11,8 @@ -(defmacro $ (expr) -"Quickly create functions from an expression EXPR with 'blanks' in +(defmacro $$ (expr) + "Quickly create functions from an expression EXPR with 'blanks' in it. Each blank is a symbol that betins with the underscore _. Symbols with the same name are treated as the same variable. @@ -19,7 +20,10 @@ A function is returned, it accepts exactly the number of variables as there were unique blanks in the expression. When calling the new function, the variables are bound in the order they appeared in EXPR. -> (macroexpand-1 '($ (+ $a (* $b 3) $b (- $a $c) 10))) +This is a rather simple macro - you cannot nest $$ forms. If you try, +an error will be raised. + +> (macroexpand-1 '($$ (+ $a (* $b 3) $b (- $a $c) 10))) (LAMBDA ($A $B $C) (+ $A (* $B 3) @@ -30,7 +34,7 @@ function, the variables are bound in the order they appeared in EXPR. The macro is useful for succinctly passing functions to higher order functions: -> (mapcar ($ (+ $ 10)) '(1 2 3 4)) +> (mapcar ($$ (+ $ 10)) '(1 2 3 4)) (11 12 13 14) > (let ((elt-num 2)) @@ -38,13 +42,18 @@ higher order functions: (list \"hey dude\" #(1 2 3 4) \"ffffffffff\"))) - (#\y 3 #\f) -" - (let ((new-params (list))) + (#\y 3 #\f)" + (let ((new-params (list)) + (calls-to-$$ 0)) (subst-if t (constantly nil) expr - :key (lambda (x) (when (is-substitute-var x) - (pushnew x new-params)))) - `(lambda ,(reverse new-params) ,expr))) + :key (lambda (x) + (when (is-substitute-var x) + (pushnew x new-params)) + (when (eql '$$ x) + (incf calls-to-$$)))) + (if (not (zerop calls-to-$$)) + (error "$$ cannot be nested") + `(lambda ,(reverse new-params) ,expr)))) |