On this page:
top
up

4.8SequencingπŸ”— i

Racket programmers prefer to write programs with as few side-effects as possible, since purely functional code is more easily tested and composed into larger programs. Interaction with the external environment, however, requires sequencing, such as when writing to a display, opening a graphical window, or manipulating a file on disk.

4.8.1Effects Before: begin πŸ”— i

+Sequencing: begin, begin0, and begin-for-syntax in The Racket Reference also documents begin .

A begin expression sequences expressions:

(begin expr...+)

The exprs are evaluated in order, and the result of all but the last expr is ignored. The result from the last expr is the result of the begin form, and it is in tail position with respect to the begin form.

Examples:
(define (print-triangleheight)
(if (zero? height)
(void )
(display (make-string height#\*))
(print-triangle(sub1 height)))))
> (print-triangle4)

****

***

**

*

Many forms, such as lambda or cond support a sequence of expressions even without a begin . Such positions are sometimes said to have an implicit begin.

Examples:
(define (print-triangleheight)
(cond
[(positive? height)
(display (make-string height#\*))
(print-triangle(sub1 height))]))
> (print-triangle4)

****

***

**

*

The begin form is special at the top level, at module level, or as a body after only internal definitions. In those positions, instead of forming an expression, the content of begin is spliced into the surrounding context.

Example:
> (let ([curly0])
(define moe(+ 1curly))
(define larry(+ 1moe)))
(list larrycurlymoe))

'(2 0 1)

This splicing behavior is mainly useful for macros, as we discuss later in Macros.

4.8.2Effects After: begin0 πŸ”— i

+Sequencing: begin, begin0, and begin-for-syntax in The Racket Reference also documents begin0 .

A begin0 expression has the same syntax as a begin expression:

(begin0 expr...+)

The difference is that begin0 returns the result of the first expr, instead of the result of the last expr. The begin0 form is useful for implementing side-effects that happen after a computation, especially in the case where the computation produces an unknown number of results.

Examples:
(define (log-timesthunk)
(thunk)
> (log-times(lambda ()(sleep 0.1)0))

Start: 1755384938719.1182

End..: 1755384938819.1387

0

> (log-times(lambda ()(values 12)))

Start: 1755384938820.0898

End..: 1755384938820.1003

1

2

4.8.3Effects If...: when and unless πŸ”— i

+Guarded Evaluation: when and unless in The Racket Reference also documents when and unless .

The when form combines an if -style conditional with sequencing for the “then” clause and no “else” clause:

(when test-exprthen-body...+)

If test-expr produces a true value, then all of the then-bodys are evaluated. The result of the last then-body is the result of the when form. Otherwise, no then-bodys are evaluated and the result is #<void>.

The unless form is similar:

(unless test-exprthen-body...+)

The difference is that the test-expr result is inverted: the then-bodys are evaluated only if the test-expr result is #f.

Examples:
(define (enumeratelst)
(if (null? (cdr lst))
(printf "~a.\n"(car lst))
(printf "~a, "(car lst))
(when (null? (cdr (cdr lst)))
(printf "and "))
(enumerate(cdr lst)))))
> (enumerate'("Larry""Curly""Moe"))

Larry, Curly, and Moe.

(define (print-triangleheight)
(unless (zero? height)
(display (make-string height#\*))
(print-triangle(sub1 height))))
> (print-triangle4)

****

***

**

*

top
up

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /