Outdated egg!
This is an egg for CHICKEN 3, the unsupported old release. You're almost certainly looking for the CHICKEN 4 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.
Introduction
A form of begin that resembles the Common Lisp tagbody construct.
Examples
Example 1 (tagged-begin returns (void))
(use tagged-begin) (let ([i 0]) (tagged-begin loop (set! i (+ i 1)) (if (< i 41) (go loop))) i) => 41
Example 2 (tagged-begin returns 42)
(use tagged-begin) (let ([i 0]) (tagged-begin loop (set! i (+ i 1)) (if (< i 42) (go loop)) (return i))) => 42
Example 3 (tagged-begin returns 43)
(use tagged-begin) (let ([i 0]) (tagged-begin loop (set! i (+ i 1)) (go b) a (if (< i 43) (go loop)) (return i) b (go a))) => 43
Example 4 (http://www.emacswiki.org/cgi-bin/wiki.pl?StateMachine)
(use tagged-begin) (begin (define (display/nl v) (display v) (newline)) (let ((a 0)) (tagged-begin start (set! a 0) part-1 (set! a (+ a 1)) (display/nl a) (cond ((>= a 9) (go end)) ((even? a) (go part-1)) (else (go part-2))) part-2 (set! a (+ a 1)) (go part-1) end (display/nl "We're done printing the odd numbers between 0 and 10"))))
This has the following output:
1 3 5 7 9 We're done printing the odd numbers between 0 and 10
Example 5 ( Knuth: "The Art of Computer Programming", vol1, p.176): Inplace inversion of a permutation represented as a vector.
(use tagged-begin) (begin (define permutation (vector 'dummy 6 2 1 5 4 3)) ; (Knuth counts from 1 not 0 :-) ) (define n (- (vector-length permutation) 1)) (define (X i) (vector-ref permutation i)) (define (X! i j) (vector-set! permutation i j)) (let ([m 0] [i 0] [j 0]) (tagged-begin I1 ; Initialize (set! m n) (set! j -1) I2 ; Next element (set! i (X m)) (if (< i 0) (go I5)) I3 ; Invert one (X! m j) (set! j (- m)) (set! m i) (set! i (X m)) I4 ; End of cycle? (if (> i 0) (go I3)) (set! i j) I5 ; Store final value (X! m (- i)) I6 ; Loop on m (set! m (- m 1)) (if (> m 0) (go I2)))) permutation) => #(dummy 3 2 6 5 4 1)
Example 6 (The CommonLisp Hyper Spec examples of tagbody)
(use tagged-begin) (begin (define val 'foo) (tagged-begin (set! val 1) (go a) c (set! val (+ val 4)) (go b) (set! val (+ val 32)) a (set! val (+ val 2)) (go c) (set! val (+ val 64)) b (set! val (+ val 8))) (define (f1 flag) (let ((n 1)) (tagged-begin (set! n (f2 flag (lambda () (go out)))) out (return n)))) (define (f2 flag escape) (if flag (escape) 2)) (list val (f1 #f) (f1 #t))) => (15 2 1)
Example 7: Demonstrates lexical scoping of tagged-begins, and that an inner tagged-begin can use an outer tag.
(use tagged-begin) (tagged-begin a (tagged-begin (go b)) b (return 'hello-world)) => 'hello-world
Example 8: Demonstrates that tags are lexically shadowed.
(use tagged-begin) (tagged-begin a (tagged-begin (go b) (return 'wrong) b (go c)) b (return 'wrong) c (return 'correct)) => 'correct
Expansion example:
(use tagged-begin) (pp (macroexpand '(tagged-begin (set! val 1) (go a) c (set! val (+ val 4)) (go b) (set! val (+ val 32)) a (set! val (+ val 2)) (go c) (set! val (+ val 64)) b (set! val (+ val 8)))))
This has the following output:
((call/cc
(lambda (go)
(let ((return (lambda (v) (go (lambda () v)))))
(letrec ((g14 (lambda () (set! val 1) (go a) (c)))
(c (lambda ()
(set! val (+ val 4))
(go b)
(set! val (+ val 32))
(a)))
(a (lambda ()
(set! val (+ val 2))
(go c)
(set! val (+ val 64))
(b)))
(b (lambda () (set! val (+ val 8)) (return (void)))))
(g14))))))
Authors
Jens Axel Søgaard, low-level macro implementation by Felix Winkelmann.
License
Copyright (c) 2005, Jens Axel Søgaard 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.
Documentation
This is a little macro that resembles the Common Lisp tagbody construct. See also "Applications of Continuations" of Daniel P. Friedman.
Motivation
Many algorithms is specified in an imperative manner in the literature (see Example 5 from Knuth). For a no-brain- conversion to Scheme tagged-begin is convenient.
Syntax
[syntax] (tagged-begin (''tag'' | ''expression'')* )
where tag is a symbol and duplicate tags are not allowed.
Semantics
The form evaluates the expressions in a lexical environment that provides functions go and return both of one argument to transfer control.
The expressions in tagged-begin are evaluated sequentially. If no expressions are left (void) is returned.
If an expression evaluates (go ''tag'') then control is transfered to the expression following tag. The tags have lexical scope. The dynamic extent of tag is indefinite. An (go ''tag'') is allowed to transfer control to an outer tagbody. The call (go ''tag'') has the proper tail recursive property, even in a situation where the call syntactically is not in tail position.
If (return ''expression'') is evaluted, the value of expression is the value of the entire tagged-begin form.
Version History
- 1.1: tagged-begin.scm needed srfi-1
- 1.0