You are looking at historical revision 29548 of this page. It may differ significantly from its current revision.

list-bindings

This module provides 6 binding constructs, including a version of Common Lisp's destructuring-bind but with shorter name, and - as an application - define-macro. 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-macro

[syntax] (define-macro (name . args) (renaming (prefix . prefixed-identifiers) (comparing keyword-predicate body)))
[syntax] (define-macro (name . args) (renaming (prefix . prefixed-identifiers) body))
[syntax] (define-macro (name . args) (injecting identifiers (comparing keyword-predicates body)))
[syntax] (define-macro (name . args) (injecting identifiers body))
[syntax] (define-macro (name . args) (comparing keyword-predicate body))
[syntax] (define-macro (name . args) body)
        

where renaming and injecting and comparing are keywords, prefixed-identifiers is a list, where all identifiers share the same prefix, identifiers is a list, keyword-predicates is either null or of the form (suffix . suffixed-keywords), suffixed-keywords is a list, where all identifiers share the same suffix.

Simplifies implicit-renaming macros by destructuring the macro-code automatically. The defined macro is either implicit- or explicit-renaming depending on the chosen keyword injecting or renaming. The macro either provides identifiers bound to itself injected or prefixed-identifiers bound to itself with the prefixed stripped and renamed. Moreover the macro provides keyword-predicates, suffixed-keywords, to check if a symbol compares to the keyword-predicate's name with its suffix stripped. The short-hand versions simply add the missing clauses with empty lists, where a missing injecting or renaming clause is interpreted as injecting.

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-macro (ifreeze xpr)
	`(lambda () ,xpr))
((ifreeze 5)) ; -> 5
(define-macro (alambda args xpr . xprs)
	(injecting (self)
		`(letrec ((,self (lambda ,args ,xpr ,@xprs)))
			 ,self)))
(define ! (alambda (n) (if (zero? n) 1 (* n (self (- n 1))))))
(! 5) ; -> 120
(define-macro (foo pair)
	(comparing (? bar?) `(if ,(bar? (car pair)) ,@(cdr pair) 'unchecked)))
(foo (bar 'checked)) ; -> 'checked
(foo (baz 'checked)) ; -> 'unchecked)
(define-macro (baz pair)
	(renaming (% %if)
		(comparing (? bar?)
			`(,%if ,(bar? (car pair)) ,@(cdr pair) 'unchecked))))
(baz (bar 'checked)) ; -> 'checked
(baz (foo 'checked)) ; -> 'unchecked
(define-macro (swap! x y)
  `(let ((tmp ,x)) (set! ,x ,y) (set! ,y tmp)))
(let ((x 'x) (y 'y)) (swap! x y) (list x y))
  ; -> '(y x)

Last update

Aug 08, 2013

Author

Juergen Lorenz

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.3
define-macro now incorporates define-ir-macro and define-er-macro, which are no longer exported
1.2
define-macro added
1.1
bind-let and bind-let* added
1.0
initial import