Begin


Purpose

"Begin" is used when you wish to evaluate a series of expressions, one after another.

Usage

"Begin" takes any number of arguments, and evalutes them one by one, in order. It returns the value of the last expression.

Examples

A simple example:
ox --> (begin (define a 1) 
	      (define b 2) 
	      (+ a b))
3
Why would it be important to be able to do this, instead of simply typing (or loading) the three expressions? In some cases, you can only evaluate a single expression, such as in an if, cond, or while statement.

Remember, with the "if" statement, if the "test" evaluates to true, then the next expression is evaluated. But if we wish several expressions to be evaluated, we must use a begin statement. Here is a world in which we would like our sphere to either be blue and wide, or red and tall (50/50 chance), and to tell us which is chosen:

  ox --> (world
           (if (< (rand) .5)
             (begin
		(print "Blue and Wide!")
		(color (vec3 0 0 1))
		(scale (vec3 3 1 1)))
             (begin
		(print "Red and Tall!")
		(color (vec3 1 0 0))
		(scale (vec3 1 3 1)))
             )
           (sphere)
          )
  ()

Return to Scheme Introduction


mrl