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

Last change on this file since 29499 was 29499, checked in by juergen, 7 years ago

define-macros syntax

File size: 5.7 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-ientifiers) xpr . xprs)))</syntax>
56         
57Simplifies implicit-renaming macros by destructuring
58the macro-form (name . args), injecting the identifiers
59and providing predicates to check if a symbol compares to
60the predicate's name with its suffix stripped
61
62==== define-er-macro
63
64<syntax>(define-er-macro (name . args) (renaming (prefix . prefixed-identifiers) (comparing ()|(suffix . suffixed-ientifiers) xpr . xprs)))</syntax>
65         
66Simplifies explicit-renaming macros by destructuring
67the macro-form (name . args), binding prefixed-identifiers
68to its own name but with prefix stripped and providing
69predicates to check if a symbol compares to the predicate's
70name with the suffix stripped
71
72=== Requirements
73
74None
75
76=== Examples
77
78<enscript highlight=scheme>
79
80(use list-bindings)
81
82(bind a 1 a) ; -> 1
83(bind (a b) '(1 2)  (list a b)) ; -> '(1 2)
84(bind (x y z w) '(1 2 3 4) (list x y z w)) ; -> '(1 2 3 4)
85(bind (x (y (z . u) . v) . w)
86        '(1 (2 (3 4) 5) 6)
87        (list x y z u v w)) ; -> '(1 2 3 (4) (5) (6))
88(bind-case '(1 (2 3))
89        ((x (y z)) (list x y z))
90        ((x (y . z)) (list x y z))
91        ((x y) (list x y))) ; -> '(1 2 3)
92(bind-case '(1 (2 3))
93        ((x (y . z)) (list x y z))
94        ((x y) (list x y))
95        ((x (y z)) (list x y z))) ; -> '(1 2 (3)))
96(bind-case '(1 (2 3))
97        ((x y) (list x y))
98        ((x (y . z)) (list x y z))
99        ((x (y z)) (list x y z))) ; -> '(1 (2 3))
100(bind-case '(1 (2 . 3))
101        ((x y) (list x y))
102        ((x (y . z)) (list x y z))
103        ((x (y z)) (list x y z))) ; -> '(1 (2 . 3))
104(letrec (
105        (my-map
106                (lambda (fn lst)
107                        (bind-case lst
108                                (() '())
109                                ((x . xs) (cons (fn x) (map fn xs))))))
110        )
111        (my-map add1 '(1 2 3))) ; -> '(2 3 4)
112((bindable? (a b)) '(1 2)) ; -> #t
113((bindable? (a . b)) '(1)) ; -> #t
114((bindable? (x)) '(name 1)) ;-> #f
115((bindable? (_ x)) '(name 1)) ; -> #t
116((bindable? (_ x)) '(name 1 2)) ;-> #f
117((bindable? (_) (_ x y)  (_ x y)) '(name 1 2)) ; -> #t
118(define-er-macro (efreeze xpr)
119        (renaming (% %lambda)
120                (comparing ()
121                        `(,%lambda () ,xpr))))
122((efreeze 3)) ; -> 3
123(define-ir-macro (ifreeze xpr)
124        (injecting ()
125                (comparing ()
126                        `(lambda () ,xpr))))
127((ifreeze 5)) ; -> 5
128(define-ir-macro (alambda args xpr . xprs)
129        (injecting (self)
130                (comparing ()
131                        `(letrec ((,self (lambda ,args ,xpr ,@xprs)))
132                                 ,self))))
133(define ! (alambda (n) (if (zero? n) 1 (* n (self (- n 1))))))
134(! 5) ; -> 120
135(define-ir-macro (foo pair)
136        (injecting ()
137                (comparing (? bar?) `(if ,(bar? (car pair)) ,@(cdr pair) 'unchecked))))
138(foo (bar 'checked)) ; -> 'checked
139(foo (baz 'checked)) ; -> 'unchecked)
140(define-er-macro (baz pair)
141        (renaming (% %if)
142                (comparing (? bar?)
143                        `(,%if ,(bar? (car pair)) ,@(cdr pair) 'unchecked))))
144(baz (bar 'checked)) ; -> 'checked
145(baz (foo 'checked)) ; -> 'unchecked
146
147</enscript>
148
149== Last update
150
151Aug 04, 2013
152
153== Author
154
155[[/users/juergen-lorenz|Juergen Lorenz]]
156
157== License
158
159 Copyright (c) 2011-2013, Juergen Lorenz
160 All rights reserved.
161
162 Redistribution and use in source and binary forms, with or without
163 modification, are permitted provided that the following conditions are
164 met:
165 
166 Redistributions of source code must retain the above copyright
167 notice, this list of conditions and the following disclaimer.
168 
169 Redistributions in binary form must reproduce the above copyright
170 notice, this list of conditions and the following disclaimer in the
171 documentation and/or other materials provided with the distribution.
172 Neither the name of the author nor the names of its contributors may be
173 used to endorse or promote products derived from this software without
174 specific prior written permission.
175   
176 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
177 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
178 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
179 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
180 HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
181 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
182 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
183 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
184 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
185 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
186 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
187
188== Version History
189
190; 1.0 : initial import
Note: See TracBrowser for help on using the repository browser.