[previous] [up] [next]     [contents]
Next: Intermezzo 1: Structures Up: Syntax and Semantics Previous: Errors

Variable Definitions

When you are defining only programs, the order of definitions does not matter. For example, both

(define (employee-name x)
  (cond 
   [(employee-record? x) (first (rest x))] 
   [else (error ``expected an employee record'')]))

(define (employee-record? y) (and (cons? y) (eq? (first y) 'EmployeeRecord)))


and
(define (employee-record? y)
  (and (cons? y) (eq? (first y) 'EmployeeRecord)))

(define (employee-name x) (cond [(employee-record? x) (first (rest x))] [else (error ``expected an employee record'')]))


have the same effect, even though employee-name uses employee-record?. However, the order for variable definitions is significant, because DrScheme evaluates the right-hand side immediately, without looking at the remaining definitions. Therefore,
(define RADIUS 5)
(define DIAMETER (* 2 RADIUS)) 

is fine, but
(define DIAMETER (* 2 RADIUS))
(define RADIUS 5) 

produces the error ``reference to undefined identifier: RADIUS''. DrScheme does not yet know the definition of RADIUS when it tries to evaluate (* 2 RADIUS).



PLT