source: project/tagged-begin/tagged-begin.html @ 1

Last change on this file since 1 was 1, checked in by azul, 15 years ago

Import everything.

File size: 7.9 KB
Line 
1<html><head><title>tagged-begin</title>
2
3<style type="text/css">
4  <!--
5      CODE {
6             color: #666666;
7           }
8      EM {
9           font-weight: bold;
10           font-style: normal;
11         }
12      DT.function { 
13                    background: #f5f5f5;
14                    color: black;
15                    padding: 0.1em;
16                    border: 1px solid #bbbaaf;
17                    font-family: monospace;
18                  }
19      PRE {
20        background: #efeee0;
21        padding: 0.1em;
22        border: 1px solid #bbbaaf;
23      }
24    -->
25</style></head>
26  <body>
27
28<center><img src="egg.jpg"></center>
29<center><a href="index.html">back</a></center>
30
31<h2>tagged-begin</h2>
32
33<h3>Description:</h3>
34
35A form of <code>begin</code> that resembles the Common Lisp <code>tagbody</code> construct.
36
37<h3>Author:</h3>
38Jens Axel S&oslash;gaard, low-level macro implementation by <a href="mailto:felix@call-with-current-continuation.org">
39felix</a>.
40
41<h3>Version:</h3>
42<ul>
43<li>1.1
44<code>tagged-begin.scm</code> needed srfi-1
45<li>1.0
46</li></ul>
47
48<h3>Usage:</h3>
49<pre>(require-extension tagged-begin)
50</pre>
51
52<h3>Download:</h3>
53<a href="tagged-begin.egg">tagged-begin.egg</a>
54
55<h3>Documentation:</h3>
56
57This is a little macro that resembles the Common Lisp
58 <a href="http://www-2.cs.cmu.edu/Groups/AI/html/hyperspec/HyperSpec/Body/speope_tagbody.html#tagbody">tagbody</a>
59construct.
60See also "Applications of Continuations" of Daniel P. Friedman.
61
62<p>MOTIVATION
63
64<p>Many algorithms is specified in an imperative manner
65in the literature [See Example 5 from Knuth]. For a no-brain-
66conversion to Scheme tagged-begin is convenient.
67
68<p>SYNTAX
69
70<p><pre>
71  (tagged-begin
72    (&lt;tag&gt; | &lt;expression&gt;)* )   
73</pre>
74
75where &lt;tag&gt; is a symbol and duplicate tags are not allowed.
76
77<p>SEMANTICS
78
79<p>The form evaluates the expressions in a lexical environment
80that provides functions <code>go</code> and <code>return</code> both of one argument to
81transfer control.
82
83<p>The expressions in <code>tagged-begin</code> are evaluated sequentially.
84If no expressions are left <code>(void)</code> is returned.
85
86<p>If an expression evaluates <code>(go tag)</code> then control is transfered
87to the expression following tag. The tags have lexical scope.
88The dynamic extent of tag is indefinite.  An <code>(go tag)</code> is allowed
89to transfer control to an outer tagbody. The call <code>(go tag)</code> has the
90proper tail recursive property, even in a situation where the call
91syntactically is not in tail position.
92
93<p>If <code>(return &lt;expression&gt;)</code> is evaluted, the value of &lt;expression&gt; is
94the value of the entire tagged-begin form.
95
96
97<h3>Examples:</h3>
98
99<pre>
100;;; EXAMPLES
101
102(define (display/nl v)
103  (display v)
104  (newline))
105
106; Example 1 (tagged-begin returns (void))
107(display/nl
108 (let ([i 0])
109   (tagged-begin
110    loop (set! i (+ i 1))
111         (if (&lt; i 41) (go loop)))
112   i))
113
114; Example 2 (tagged-begin returns 42)
115(display/nl
116 (let ([i 0])
117   (tagged-begin
118    loop (set! i (+ i 1))
119         (if (&lt; i 42) (go loop))
120         (return i))))
121
122; Example 3 (tagged-begin returns 43)
123(display/nl
124 (let ([i 0])
125   (tagged-begin
126    loop (set! i (+ i 1))
127         (go b)
128    a    (if (&lt; i 43) (go loop))
129         (return i)
130    b    (go a))))
131
132; Example 4 ( &lt;http://www.emacswiki.org/cgi-bin/wiki.pl?StateMachine&gt; )
133
134(let ((a 0))
135   (tagged-begin
136     start
137      (set! a 0)
138     part-1
139      (set! a (+ a 1))
140      (display/nl a)
141      (cond
142        ((&gt;= a  9)  (go end))
143        ((even? a)  (go part-1))
144        (else       (go part-2)))
145     part-2
146      (set! a (+ a 1))
147      (go part-1)
148     end
149      (display/nl "We're done printing the odd numbers between 0 and 10")))
150
151; Example 5 ( Knuth: "The Art of Computer Programming", vol1, p.176)
152
153; Inplace inversion of a permutation represented as a vector.
154
155(define permutation (vector 'dummy 6 2 1 5 4 3))      ; (Knuth counts from 1 not 0 :-) )
156(define n           (- (vector-length permutation) 1))
157(define (X i)       (vector-ref permutation i))
158(define (X! i j)    (vector-set! permutation i j))
159
160(let ([m 0] [i 0] [j 0])
161  (tagged-begin
162   I1   ; Initialize
163        (set! m n)
164        (set! j -1)
165   I2   ; Next element
166        (set! i (X m))
167        (if (&lt; i 0) (go I5))
168   I3   ; Invert one
169        (X! m j)
170        (set! j (- m))
171        (set! m i)
172        (set! i (X m))
173   I4   ; End of cycle?
174        (if (&gt; i 0) (go I3))
175        (set! i j)
176   I5   ; Store final value
177        (X! m (- i))
178   I6   ; Loop on m
179        (set! m (- m 1))
180        (if (&gt; m 0) (go I2))))
181
182(display/nl permutation)
183
184; Example 6 (The CommonLisp Hyper Spec examples of tagbody)     
185 
186(define val 'foo)
187(tagged-begin
188    (set! val 1)
189       (go a)
190 c     (set! val (+ val 4))
191       (go b)
192       (set! val (+ val 32))
193 a     (set! val (+ val 2))
194       (go c)
195       (set! val (+ val 64))
196 b     (set! val (+ val 8)))
197(display/nl val)
198
199(define (f1 flag)
200  (let ((n 1))
201    (tagged-begin
202     (set! n (f2 flag (lambda () (go out))))
203     out
204     (display n))))
205
206(define (f2 flag escape)
207  (if flag (escape) 2))
208
209(display/nl (f1 #f))
210(display/nl (f1 #t))
211
212; Example 7
213;   Demonstrates lexical scoping of tagged-begins,
214;   and that an inner tagged-begin can use an outer tag.
215
216(pp (tagged-begin
217 a (tagged-begin
218      (go b))
219 b (return 'hello-world))
220)
221
222; Demonstrates that tags are lexically shadowed.
223(pp (tagged-begin
224 a (tagged-begin
225        (go b)
226        (return 'wrong)
227      b (go c))
228 b (return 'wrong)
229 c (return 'correct))
230)
231
232
233;;; EXPANSION EXAMPLE           
234           
235;(tagbody
236;       (set! val 1)
237;       (go a)
238; c     (set! val (+ val  4))
239;       (go b)
240;       (set! val (+ val 32))
241; a     (set! val (+ val  2))
242;       (go c)
243;       (set! val (+ val 64))
244; b     (set! val (+ val  8)))
245
246; expands to           
247           
248;((call/cc
249;  (lambda (go)
250;   (let ([return (lambda (v) (go (lambda () v)))])
251;     (letrec
252;         ([start (lambda ()
253;                   (set! val 1)
254;                   (go a)
255;                   (c))]
256;          [c      (lambda ()
257;                    (set! val (+ val 4))
258;                    (go b)
259;                    (set! val (+ val 32))
260;                    (a))]
261;          [a      (lambda ()
262;                    (set! val (+ val 2))
263;                    (go c)
264;                    (set! val (+ val 64))
265;                    (b))]
266;          [b      (lambda ()
267;                    (set! val (+ val 8)))])
268;       (start))))))
269</pre>
270
271
272<h3>License:</h3>
273
274<pre>
275Copyright (c) 2005, Jens Axel S&oslash;gaard
276All rights reserved.
277
278Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
279conditions are met:
280
281  Redistributions of source code must retain the above copyright notice, this list of conditions and the following
282    disclaimer.
283  Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
284    disclaimer in the documentation and/or other materials provided with the distribution.
285  Neither the name of the author nor the names of its contributors may be used to endorse or promote
286    products derived from this software without specific prior written permission.
287
288THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
289OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
290AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
291CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
292CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
293SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
294THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
295OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
296POSSIBILITY OF SUCH DAMAGE.
297</pre>
298
299
300<hr><a href="index.html">back</a>
301
302
303</body></html>
Note: See TracBrowser for help on using the repository browser.