Comp210: Principles of Computing and Programming
Fall 2004 -- Lecture #31, Supplemental Material   


(begin expr1 expr2 expr3 ...)

The begin statement enables us to execute a sequence of expressions. expr1 is executed then expr2 then expr3, etc. The return value is the value of the last expression.

begin is particularly useful when using mutation, as set-struct-field! and set! have void (no) return values.

Example (moves p to a new position):

(define p (make-posn 10 15))

      

(begin

    (set-posn-x! p 3)

    (set-posn-y! p -2)

    "p was modified!")

	

(if predicate trueExpr falseExpr)

The if function is a simplified cond statement with only 2 cases. It is equivalent to



(cond

   [predicate trueExpr]

   [else falseExpr])

   

Example (returns the absolute value of x):

(if (> x 0) 

    x 

    (* -1 x))

	

 


Last Revised Tuesday, 24-Aug-2004 13:49:10 CDT

©2004 Stephen Wong and Dung Nguyen