source: project/wiki/eggref/4/list-bindings @ 29509

Last change on this file since 29509 was 29509, checked in by juergen, 6 years ago

some better names in list-bindings

File size: 6.4 KB
Line 
1[[tags: egg]]
2[[toc:]]
3
4== list-bindings
5
6This module provides 4 binding constructs, including a version of Common Lisp's
7destructuring-bind but with shorter name, as well as two macros to
8simplyfy the definition of low-level macros. Contrary to the bindings
9egg, destructuring is only done for list expressions, which is
10sufficient for low-level macros.
11
12=== Programming interface
13
14==== list-bindings
15
16<procedure>(list-bindings [sym])</procedure>
17
18documentation procedure. Without sym procuces a list of exported symbols
19and with sym that sym's documentation
20
21====  bind
22
23<syntax>(bind pat seq xpr . xprs)</syntax>
24
25binds pattern variables of pat, a nested lambda-list, to subexpressions
26of the list expression seq and executes xpr . xprs in this context
27
28==== bindable?
29
30<syntax>(bindable? pat . pats)</syntax>
31
32returns a predicate which checks, if its only list argument matches
33any of the patterns pat . pats.
34
35====  bind-case
36
37<syntax>(bind-case seq clause . clauses)</syntax>
38
39where seq is a list expression and each clause is of the form
40(pat xpr . xprs), with pat a nested lambda-list.
41
42Matches seq against each pattern pat in sequence and executes the
43corresponding body xpr . xprs of the first match.
44
45==== bind/cc
46
47<syntax>(bind/cc cont xpr . xprs)</syntax>
48
49syntactic sugar for call-with-current-continuation. Captures the current
50contiunuation, binds it to cont and evaluates the body xpr . xprs in
51this context.
52
53==== define-ir-macro
54
55<syntax>(define-ir-macro (name . args) (injecting identifiers (comparing ()|(suffix . suffixed-keywords) xpr . xprs)))</syntax>
56         
57where injecting and comparing are keywords.
58Simplifies implicit-renaming macros by destructuring
59the macro-form (name . args), injecting the identifiers
60and providing predicates, suffixed-keywords, to check if a symbol
61compares to the predicate's name with its suffix stripped.
62Note, that this syntax shows nicely in the header, if there are injected
63identifiers, i.e. if the macro is hygienic or not, and what the
64additional keywords are, namely the suffixed-keywords with suffix
65stripped.
66
67==== define-er-macro
68
69<syntax>(define-er-macro (name . args) (renaming (prefix . prefixed-identifiers) (comparing ()|(suffix . suffixed-keywords) xpr . xprs)))</syntax>
70         
71where renaming and comparing are keywords.
72Simplifies explicit-renaming macros by destructuring
73the macro-form (name . args), binding prefixed-identifiers
74to its own name but with prefix stripped and providing
75predicates, suffixed-keywords, to check if a symbol compares to the
76predicate's name with the suffix stripped.
77Note, that contrary to the identifiers list in define-ir-macro, the list
78prefixed-identifiers exposes information of the macro's implementation;
79this list is usually updated in the process of macro-writing.
80The list suffixed-keywords, on the other hand, provides information on the
81additional keywords of the macro, if any: Simply strip the suffix.
82
83=== Requirements
84
85None
86
87=== Examples
88
89<enscript highlight=scheme>
90
91(use list-bindings)
92
93(bind a 1 a) ; -> 1
94(bind (a b) '(1 2)  (list a b)) ; -> '(1 2)
95(bind (x y z w) '(1 2 3 4) (list x y z w)) ; -> '(1 2 3 4)
96(bind (x (y (z . u) . v) . w)
97        '(1 (2 (3 4) 5) 6)
98        (list x y z u v w)) ; -> '(1 2 3 (4) (5) (6))
99(bind-case '(1 (2 3))
100        ((x (y z)) (list x y z))
101        ((x (y . z)) (list x y z))
102        ((x y) (list x y))) ; -> '(1 2 3)
103(bind-case '(1 (2 3))
104        ((x (y . z)) (list x y z))
105        ((x y) (list x y))
106        ((x (y z)) (list x y z))) ; -> '(1 2 (3)))
107(bind-case '(1 (2 3))
108        ((x y) (list x y))
109        ((x (y . z)) (list x y z))
110        ((x (y z)) (list x y z))) ; -> '(1 (2 3))
111(bind-case '(1 (2 . 3))
112        ((x y) (list x y))
113        ((x (y . z)) (list x y z))
114        ((x (y z)) (list x y z))) ; -> '(1 (2 . 3))
115(letrec (
116        (my-map
117                (lambda (fn lst)
118                        (bind-case lst
119                                (() '())
120                                ((x . xs) (cons (fn x) (map fn xs))))))
121        )
122        (my-map add1 '(1 2 3))) ; -> '(2 3 4)
123((bindable? (a b)) '(1 2)) ; -> #t
124((bindable? (a . b)) '(1)) ; -> #t
125((bindable? (x)) '(name 1)) ;-> #f
126((bindable? (_ x)) '(name 1)) ; -> #t
127((bindable? (_ x)) '(name 1 2)) ;-> #f
128((bindable? (_) (_ x y)  (_ x y)) '(name 1 2)) ; -> #t
129(define-er-macro (efreeze xpr)
130        (renaming (% %lambda)
131                (comparing ()
132                        `(,%lambda () ,xpr))))
133((efreeze 3)) ; -> 3
134(define-ir-macro (ifreeze xpr)
135        (injecting ()
136                (comparing ()
137                        `(lambda () ,xpr))))
138((ifreeze 5)) ; -> 5
139(define-ir-macro (alambda args xpr . xprs)
140        (injecting (self)
141                (comparing ()
142                        `(letrec ((,self (lambda ,args ,xpr ,@xprs)))
143                                 ,self))))
144(define ! (alambda (n) (if (zero? n) 1 (* n (self (- n 1))))))
145(! 5) ; -> 120
146(define-ir-macro (foo pair)
147        (injecting ()
148                (comparing (? bar?) `(if ,(bar? (car pair)) ,@(cdr pair) 'unchecked))))
149(foo (bar 'checked)) ; -> 'checked
150(foo (baz 'checked)) ; -> 'unchecked)
151(define-er-macro (baz pair)
152        (renaming (% %if)
153                (comparing (? bar?)
154                        `(,%if ,(bar? (car pair)) ,@(cdr pair) 'unchecked))))
155(baz (bar 'checked)) ; -> 'checked
156(baz (foo 'checked)) ; -> 'unchecked
157
158</enscript>
159
160== Last update
161
162Aug 04, 2013
163
164== Author
165
166[[/users/juergen-lorenz|Juergen Lorenz]]
167
168== License
169
170 Copyright (c) 2011-2013, Juergen Lorenz
171 All rights reserved.
172
173 Redistribution and use in source and binary forms, with or without
174 modification, are permitted provided that the following conditions are
175 met:
176 
177 Redistributions of source code must retain the above copyright
178 notice, this list of conditions and the following disclaimer.
179 
180 Redistributions in binary form must reproduce the above copyright
181 notice, this list of conditions and the following disclaimer in the
182 documentation and/or other materials provided with the distribution.
183 Neither the name of the author nor the names of its contributors may be
184 used to endorse or promote products derived from this software without
185 specific prior written permission.
186   
187 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
188 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
189 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
190 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
191 HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
192 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
193 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
194 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
195 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
196 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
197 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
198
199== Version History
200
201; 1.0 : initial import
Note: See TracBrowser for help on using the repository browser.