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

list-bindings

This module provides some binding constructs, including a version of Common Lisp's destructuring-bind but with a shorter name, bind, and - as applications - define-macro, let-macro and letrec-macro. In all the macros defined here, pat is a pattern, i.e. a nested lambda-list and pattern variables are the symbols of the pattern.

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)

shows the list of exported symbols.

bind

[syntax] (bind pat xpr . body)

binds pattern variables of the pattern pat to subexpressions of the nested pseudolist expression xpr and executes body in this context.

bind-lambda

[syntax] (bind-lambda pat xpr . xprs)

combination of bind and lambda.

bind-set!

[syntax] (bind-set! pat xpr)

sets the pattern variables of the pattern pat to corresponding subexpressions of the nested pseudolist xpr.

bind-define

[syntax] (bind-define pat xpr)

defines the pattern variables of the pattern pat by setting them to corresponding subexpressions of the nested pseudolist xpr.

bind-let*

[syntax] (bind-let* ((pat xpr) ...) . body)

binds pattern variables of the pattern pat to corresponding subexpressions of the nested pseudolist expression xpr ... in sequence and executes body in this context.

bind-let

[syntax] (bind-let ((pat xpr) ...) . body)

binds pattern variables of the pattern pat to corresponding subexpressions of the nested pseudolist expression xpr ... in parallel and executes body in this context.

bindable?

[syntax] (bindable? pat)

returns a predicate which checks, if its only list argument matches the patterns pat.

bind-case

[syntax] (bind-case xpr clause . clauses)

where xpr is a nested pseudolist expression and each clause is of the form (pat . body), with pat a pattern.

Matches xpr against each pattern pat in sequence, binds the pattern variables of the first matching pat to correspondings subexpressions of xpr and executes the corresponding body in this context.

bind-case-lambda

[syntax] (bind-case-lambda (pat xpr . xprs) ...)

combination of bind-case and lambda.

bind/cc

[syntax] (bind/cc cont . body)

syntactic sugar for call-with-current-continuation. Captures the current contiunuation, binds it to cont and evaluates the body 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, injecting and comparing are auxiliary keywords, prefixed-identifiers is a list, where all of its items 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 of its items 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.

let-macro

[syntax] (let-macro ((macro-code macro-body) ...) . body)

defines macros with macro-code and macro-body ... in parallel and exectues body in this context.

letrec-macro

[syntax] (letrec-macro ((macro-code macro-body) ...) . body)

defines macros with macro-code and macro-body ... recursively and exectues body in this context.

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-lambda (a (b . c) . d) (list a b c d))
 '(1 (20 30 40) 2 3)) ; -> '(1 20 (30 40) (2 3))
(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))
((bind-case-lambda
	((a (b . c) . d) (list a b c d))
	((e . f) (list e f)))
 '(1 2 3 4 5)) ; -> '(1 (2 3 4 5))
(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
(begin
  (bind-set! (a (b . c)) '(1 (2)))
  (list a b c)) ;-> '(1 2 ())
(begin
  (bind-define (push top pop)
    (let ((state '()))
      (list
        (lambda (arg) (set! state (cons arg state)))
        (lambda () (car state))
        (lambda () (set! state (cdr state))))))
  (push 3)
  (push 5)
  (top)) ; -> 5
  
(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)
(letrec-macro (((ifreeze xpr) `(lambda () ,xpr))
							 ((efreeze xpr)
								(renaming (% %lambda)
								 `(,%lambda () ,xpr))))
 ((efreeze ((ifreeze 3)))))
 ; -> 3
(let-macro (((ifreeze xpr) `(lambda () ,xpr))
						((efreeze xpr)
						 (renaming (% %lambda)
							`(,%lambda () ,xpr))))
  (list ((efreeze 3)) ((ifreeze 5))))
  ; -> '(3 5)

Last update

Oct 06, 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.6
bind-lambda and bind-case-lambda added
1.5
bind-set! and bind-define added, code partially rewritten
1.4
let-macro and letrec-macro added
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