You are looking at historical revision 29521 of this page. It may differ significantly from its current revision.
list-bindings
This module provides 4 binding constructs, including a version of Common Lisp's destructuring-bind but with shorter name, as well as two macros to simplyfy the definition of low-level macros. Contrary to the bindings egg, destructuring is only done for list expressions, which is sufficient for low-level macros.
Programming interface
list-bindings
[procedure] (list-bindings [sym])documentation procedure. Without sym produces a list of exported symbols and with sym that sym's documentation
bind
[syntax] (bind pat seq xpr . xprs)binds pattern variables of pat, a nested lambda-list, to subexpressions of the list expression seq and executes xpr . xprs in this context
bind-let*
[syntax] (bind-let* ((pat seq) ...) xpr . xprs)binds pattern variables of pat to corresponding subexpressions of seq ... in sequence and executes body xpr . xprs in this context.
bind-let
[syntax] (bind-let ((pat seq) ...) xpr . xprs)binds pattern variables of pat to corresponding subexpressions of seq ... in parallel and executes body xpr . xprs in this context.
bindable?
[syntax] (bindable? pat . pats)returns a predicate which checks, if its only list argument matches any of the patterns pat . pats.
bind-case
[syntax] (bind-case seq clause . clauses)where seq is a list expression and each clause is of the form (pat xpr . xprs), with pat a nested lambda-list.
Matches seq against each pattern pat in sequence and executes the corresponding body xpr . xprs of the first match.
bind/cc
[syntax] (bind/cc cont xpr . xprs)syntactic sugar for call-with-current-continuation. Captures the current contiunuation, binds it to cont and evaluates the body xpr . xprs in this context.
define-ir-macro
[syntax] (define-ir-macro (name . args) (injecting identifiers (comparing keyword-predicates xpr . xprs)))where injecting and comparing are keywords and keyword-predicates is either null or of the form (suffix . suffixed-keywords). Simplifies implicit-renaming macros by destructuring the macro-form (name . args), injecting the identifiers and providing keyword-predicates, suffixed-keywords, to check if a symbol compares to the keyword-predicate's name with its suffix stripped. Note, that this syntax shows nicely in the header, if there are injected identifiers, i.e. if the macro is hygienic or not, and what the additional keywords are, namely the suffixed-keywords with suffix stripped.
define-er-macro
[syntax] (define-er-macro (name . args) (renaming (prefix . prefixed-identifiers) (comparing keyword-predicate xpr . xprs)))where renaming and comparing are keywords and keyword-predicates is either null or of the form (suffix . suffixed-keywords). Simplifies explicit-renaming macros by destructuring the macro-form (name . args), binding prefixed-identifiers to its own name but with prefix stripped and providing keyword-predicates, suffixed-keywords, to check if a symbol compares to the keyword-predicate's name with the suffix stripped. Note, that contrary to the identifiers list in define-ir-macro, the list prefixed-identifiers exposes information of the macro's implementation; this list is usually updated in the process of macro-writing. The list suffixed-keywords, on the other hand, provides information on the additional keywords of the macro, if any: Simply strip the suffix.
Requirements
None
Examples
(use list-bindings) (bind a 1 a) ; -> 1 (bind (a b) '(1 2) (list a b)) ; -> '(1 2) (bind (x y z w) '(1 2 3 4) (list x y z w)) ; -> '(1 2 3 4) (bind (x (y (z . u) . v) . w) '(1 (2 (3 4) 5) 6) (list x y z u v w)) ; -> '(1 2 3 (4) (5) (6)) (bind-let* (((a b) '(1 2)) ((x . y) '(3))) (list a b x y)) ; -> '(1 2 3 ()) (bind-let* (((a b) '(1 2)) ((x . y) (list a))) (list a b x y)) ; -> '(1 2 1 ()) (bind-let (((a b) '(1 2)) ((x . y) '(3 4 4))) (list a b x y)) ; -> '(1 2 3 (4 4)) (bind-case '(1 (2 3)) ((x (y z)) (list x y z)) ((x (y . z)) (list x y z)) ((x y) (list x y))) ; -> '(1 2 3) (bind-case '(1 (2 3)) ((x (y . z)) (list x y z)) ((x y) (list x y)) ((x (y z)) (list x y z))) ; -> '(1 2 (3))) (bind-case '(1 (2 3)) ((x y) (list x y)) ((x (y . z)) (list x y z)) ((x (y z)) (list x y z))) ; -> '(1 (2 3)) (bind-case '(1 (2 . 3)) ((x y) (list x y)) ((x (y . z)) (list x y z)) ((x (y z)) (list x y z))) ; -> '(1 (2 . 3)) (letrec ( (my-map (lambda (fn lst) (bind-case lst (() '()) ((x . xs) (cons (fn x) (map fn xs)))))) ) (my-map add1 '(1 2 3))) ; -> '(2 3 4) ((bindable? (a b)) '(1 2)) ; -> #t ((bindable? (a . b)) '(1)) ; -> #t ((bindable? (x)) '(name 1)) ;-> #f ((bindable? (_ x)) '(name 1)) ; -> #t ((bindable? (_ x)) '(name 1 2)) ;-> #f ((bindable? (_) (_ x y) (_ x y)) '(name 1 2)) ; -> #t (define-er-macro (efreeze xpr) (renaming (% %lambda) (comparing () `(,%lambda () ,xpr)))) ((efreeze 3)) ; -> 3 (define-ir-macro (ifreeze xpr) (injecting () (comparing () `(lambda () ,xpr)))) ((ifreeze 5)) ; -> 5 (define-ir-macro (alambda args xpr . xprs) (injecting (self) (comparing () `(letrec ((,self (lambda ,args ,xpr ,@xprs))) ,self)))) (define ! (alambda (n) (if (zero? n) 1 (* n (self (- n 1)))))) (! 5) ; -> 120 (define-ir-macro (foo pair) (injecting () (comparing (? bar?) `(if ,(bar? (car pair)) ,@(cdr pair) 'unchecked)))) (foo (bar 'checked)) ; -> 'checked (foo (baz 'checked)) ; -> 'unchecked) (define-er-macro (baz pair) (renaming (% %if) (comparing (? bar?) `(,%if ,(bar? (car pair)) ,@(cdr pair) 'unchecked)))) (baz (bar 'checked)) ; -> 'checked (baz (foo 'checked)) ; -> 'unchecked
Last update
Aug 05, 2013
Author
License
Copyright (c) 2011-2013, Juergen Lorenz All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Version History
- 1.1
- bind-let and bind-let* added
- 1.0
- initial import