You are looking at historical revision 30039 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 - macro-rules, 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.

macro-rules

[syntax] (macro-rules sym ... () (pat0 tpl0) (pat1 tpl1) ...)
[syntax] (macro-rules sym ... (suffix suffixed-keyword ...) (pat0 tpl0) (pat1 tpl1) ...)

This macro is inspired by syntax-rules and is used like it (after issuing import-for-syntax). The differences to syntax-rules are the following:

First, injected - hence unhygienic - symbols sym ... before the keyword list are allowed. So anaphoric macros are possible and clearly documented.

Second, if the keyword-list is nonempty, it must be of the form

 (? keyword? ...)

which means that keyword? is a predicate which checks for its own name with the suffix ? stripped (other common suffixes are allowed but unusual).

Third, the patterns pat0 pat1 ... are nested lambda-lists, i.e. ellipses have no special meaning, if used at all.

Third, the templates tpl0 tpl1 ... evaluate usually to quasiquoted expressions as is the case in most low-level macros.

once-only

[syntax] (once-only (name ...) xpr . xprs)

the variables name ... are bound to gensyms only once and from left to rigth in the macro body xpr . xprs

with-gensyms

[syntax] (with-gensyms (name ...) xpr . xprs)

the variables name ... are bound to gensyms to be used in the macro body xpr . xprs

Requirements

None

Examples


(use list-bindings)
(import-for-syntax
 (only list-bindings macro-rules once-only with-gensyms))

(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-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)

;; anaphoric if
(define-syntax aif
  (macro-rules it ()
    ((_ test consequent)
     `(let ((,it ,test))
        (if ,it ,consequent)))
    ((_ test consequent alternative)
      `(let ((,it ,test))
         (if ,it ,consequent ,alternative)))))
(aif #f it (not it)) ; -> #t
(define (mist x) (aif (! x) it))
(mist 5) ; -> 120

;; verbose if
(define-syntax vif
  (macro-rules (? then? else?)
    ((_ test (then xpr . xprs))
     `(vif ,test (then ,xpr ,@xprs) (else (void))))
    ((_ test (else xpr . xprs))
     `(vif ,test (then (void)) (else ,xpr ,@xprs)))
    ((_ test consequent alternative)
     `(if ,test
       (if ,(and (pair? consequent) (then? (car consequent)))
       ;(if (and (pair? ',consequent) (,then? ',(car consequent)))
         (begin ,@(cdr consequent)))
       (if ,(and (pair? alternative) (else? (car alternative)))
       ;(if (and (pair? ',alternative) (,else? ',(car alternative)))
         (begin ,@(cdr alternative)))))
    ))
(define (quux x)
  (vif (odd? x) (then "odd") (else "even")))
(quux 3) ; -> "odd"
(quux 4) ; -> "even"

;; using once-only and define-macro
(define-macro (square x)
	(once-only (x)
		`(* ,x ,x)))
(define-macro (for (var start end) . body)
	(once-only (start end)
		`(do ((,var ,start (add1 ,var)))
					 ((= ,var ,end))
					 ,@body)))
(define counter
	(let ((state 0))
		(lambda ()
			(set! state (+ state 1))
			state)))
(for (x 0 (counter)) (print x)) ; prints 0
; without once-only this would run forever

;; a rather contrived example for with-gensyms
(define-macro (times a b)
	(with-gensyms (x y)
		`(let ((,x ,a) (,y ,b))
			 (* ,x ,y))))

Last update

Nov 12, 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.8
define-macro corrected, once-only and with-gensyms added
1.7
macro-rules added
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