Control
1 History
2 Control Structures
while
until
dotimes
tagged-begin
begin/  goto
Index
6.12

Control

Jens Axel Søgaard <[email protected]>

 (require control) package: control

1 History

Version 2.0

- added documentation in Scribble form

Version 1.1

- added begin/goto

Version 1.0

- initial version, available control structures: while, until, dotimes, and tagged-begin

2 Control Structures

syntax

(while test body)

Syntax: test should be an expression and body a sequence of one or more expressions.

Semantics: while is an iteration construct. Each iteration begins by evaluating the test expression. If it evaluates to a true value, the body expressions are evaluated sequentially from left to right, then the next iteration begins. If the test expression evaluates to false then iteration stops and an unspecified value is returned as the result of the while-expression.

Example:
> (require control)
> (let ([n 3] [sum 0])
    (while (> n 0)
           (set! sum (+ sum n))
           (set! n (- n 1)))
    sum)

6

syntax

(until test body)

Syntax: test should be an expression and body a sequence of one or more expressions.

Semantics: until is an iteration construct. Each iteration begins by evaluating the body expressions sequentially from left to right. The test expression is then evaluated. If the result is a true value, then the next iteration begins. Otherwise the iteration stops and unspecified value is returned as the result of the until-expression.

Example:
> (require control)
> (let ([n 3] [sum 0])
    (until (= n 1)
           (set! sum (+ sum n))
           (set! n (- n 1)))
    sum)

5

syntax

(dotimes (variable expression [finally]) body)

Syntax: variable should be an identifier, expression and finally (if present) should be expressions and body a sequence of one or more expressions.

Semantics: dotimes is an iteration contructs. Evalutations begins by evaluating expression. If the result is not an integer an error is signaled. If the result is zero or negative, the body expressions are not evaluated. Otherwise the body expressions are evaluated for each integer from 0 up to but not including the result of expression.

During each evaluation of the body expressions, variable is bound to each integer.

When the iteration stops finally is evaluated if present and the result returned, otherwise void is returned. During evaluation of finally the variable is bound to the number of times the body were evaluated.

Examples:
> (require control)
> (let ((xs '()))
    (dotimes (x 5)
             (set! xs (cons x xs)))
    xs)

'(4 3 2 1 0)

> (let ((xs '()))
    (dotimes (x 5 (list xs x))
             (set! xs (cons x xs))))

'((4 3 2 1 0) 5)

syntax

(tagged-begin (tag / expression)*)

Syntax: tag should be a symbol, and all tags should be different.

Motivation: The macro tagged-begin is inspired by the Common Lisp construct tagbody.

Semantics: The tagged-begin expression evaluates the expressions in a lexical environment, where go and return are are bound to functions of one argument, which will transfer control when called.

As main rule the expressions will be evaluated sequentially from left to right. When there are no more expressions to be evaluated void is returned.

If an expression evaluates (go tag then control is transfered to the expression following the tag. The tags have lexical scope. The dynamic extent of tag is indefinite. An (go tag) is allowed to tranfer control to an outer tagged-begin. The call (go tag) has the proper tail recursive property, even in situation where the call syntactically is not in tail position.

If (return expression) is evaluted, the value of expression is returned as the value of the entire tagged-begin form.

Examples:
> (require control)
> (let ([i 0])
    (tagged-begin
     loop (set! i (+ i 1))
          (when (< i 41) (go loop)))
    i)

41

> (let ([odd-numbers '()]
        [a 0])
    (tagged-begin
     start    (set! a 0)
     on-odd   (set! a (+ a 1))
              (set! odd-numbers (cons a odd-numbers))
              (cond
                [(>= a  9)  (go end)]
                [(even? a)  (go on-even)]
                [else       (go on-odd)])
     on-even  (set! a (+ a 1))
              (go on-odd)
     end)
    odd-numbers)

'(10 8 6 4 2 1)

References: "Applications of Continuations" of Daniel P. Friedman.

syntax

(begin/goto label-or-goto-or-expression*)

Syntax: label-or-goto-or-expression is either (label identifier) or (goto identifier) or expression.

Motivation: Think of begin/goto as a normal begin, where goto can be used to jump to a control point named by a label. An (goto identifier) will transfer control to the point named by the identifier. If the goto-form is one of the label-or-goto-expression, then a goto doesn’t grow the control context.

Examples:

> (require control)
> (let ([x 1])
    (let/ec return
      (begin/goto
        (label l1)
        (set! x (+ x 1))
        (when (= x 10000000)
          (return x))
        (goto l1))))

10000000

> (let ([x 1])
    (let/ec return
      (begin/goto
        (label l1)
        (set! x (+ x 1))
        (when (= x 10000000)
          (return x))
        (goto l1)
        2)))

10000000

Index

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

 

begin/goto
CL
Control
control
control
Control Structures
dotimes
go
goto
History
iteration
knuth
label
loop
return
tagbody
tagged-begin
until
while