chickadee » list-bindings

Outdated egg!

This is an egg for CHICKEN 4, the unsupported old release. You're almost certainly looking for the CHICKEN 5 version of this egg, if it exists.

If it does not exist, there may be equivalent functionality provided by another egg; have a look at the egg index. Otherwise, please consider porting this egg to the current version of CHICKEN.

THIS MODULE IS NOW OBSOLETE. USE BINDINGS INSTEAD!

list-bindings

This module provides some binding constructs, including a version of Common Lisp's destructuring-bind but with a shorter name, bind. 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.

Enhanced versions of these macros, which can destruct arbitrary sequences and not only lists, and also provide additional where clauses, can be found in the bindings module.

The macro-building macros of former versions of this module are removed here and can now be found in low-level-macros.

Programming interface

list-bindings

list-bindingsprocedure

shows the list of exported symbols.

bind

(bind pat xpr . body)syntax

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

bind-lambda

(bind-lambda pat xpr . xprs)syntax

combination of bind and lambda.

bind-set!

(bind-set! pat xpr)syntax

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

bind-define

(bind-define pat xpr)syntax

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

bind-let*

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

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

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

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?

(bindable? pat)syntax

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

bind-case

(bind-case xpr clause . clauses)syntax

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

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

combination of bind-case and lambda.

bind/cc

(bind/cc cont . body)syntax

syntactic sugar for call-with-current-continuation. Captures the current contiunuation, binds it to cont and evaluates the body in this context.

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
  
== Last update

Jan 31, 2014

== Author

[[/users/juergen-lorenz|Juergen Lorenz]]

== License

 Copyright (c) 2011-2014, 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.9.2 : module now obsolete, use bindings instead
; 1.9 : macro-building macros removed here and added to the low-level-macros module
; 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

Contents »