You are looking at historical revision 8852 of this page. It may differ significantly from its current revision.

Introduction

A form of begin that resembles the Common Lisp tagbody construct.

Examples

<example>

 <init>(use tagged-begin)</init>
 <expr>
Example 1 (tagged-begin returns (void))
(let ([i 0])
  (tagged-begin
   loop (set! i (+ i 1))
        (if (< i 41) (go loop)))
  i)</expr>
  <result>41</result>

</example>

  

<example>

 <init>(use tagged-begin)</init>
 <expr>
Example 2 (tagged-begin returns 42)
(let ([i 0])
  (tagged-begin
   loop (set! i (+ i 1))
        (if (< i 42) (go loop))
        (return i)))</expr>
 <result>42</result>

</example>

<example>

 <init>(use tagged-begin)</init>
 <expr>
Example 3 (tagged-begin returns 43)
(let ([i 0])
  (tagged-begin
   loop (set! i (+ i 1))
        (go b)
   a    (if (< i 43) (go loop))
        (return i)
   b    (go a)))</expr>
 <result>43</result>

</example>

<example>

 <init>(use tagged-begin)</init>
 <expr>
Example 4 (http
//www.emacswiki.org/cgi-bin/wiki.pl?StateMachine)

(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")))</expr>
 <output>1

3 5 7 9 We're done printing the odd numbers between 0 and 10</output> </example>

<example>

 <init>(use tagged-begin)</init>
 <expr>
Example 5 ( Knuth
"The Art of Computer Programming", vol1, p.176)
Inplace inversion of a permutation represented as a vector.

(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</expr>

 <result>#(dummy 3 2 6 5 4 1)</result>

</example>

<example>

 <init>(use tagged-begin)</init>
 <expr>
Example 6 (The CommonLisp Hyper Spec examples of tagbody)

(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))</expr>

 <result>(15 2 1)</result>

</example>

<example>

 <init>(use tagged-begin)</init>
 <expr>
Example 7
Demonstrates lexical scoping of tagged-begins,
and that an inner tagged-begin can use an outer tag.

(tagged-begin

a (tagged-begin
     (go b))
b (return 'hello-world))

</expr>

 <result>hello-world</result>

</example>

<example>

 <init>(use tagged-begin)</init>
 <expr>
Demonstrates that tags are lexically shadowed.

(tagged-begin

a (tagged-begin
       (go b)
       (return 'wrong)
     b (go c))
b (return 'wrong)
c (return 'correct))

</expr>

 <result>correct</result>

</example>

<example>

 <init>(use tagged-begin)</init>
 <expr>
;; EXPANSION EXAMPLE
           

(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)))))</expr>
<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))))))</output>

</example>

Authors

Jens Axel S&oslash;gaard, low-level macro implementation by Felix Winkelmann.

License

Copyright (c) 2005, Jens Axel S&oslash;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:

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