[previous] [up] [next]     [index]
Next: Semantics of local Up: Organizing Programs with local Previous: Organizing Programs with local

Syntax of local

A local-expression is just another kind of expression:

tabular21784

As usual, <def-1> ... <def-n> is an arbitrarily long sequence of definitions (see figure [cross-reference]) and <exp> is an arbitrary expression. In other words, a local-expression consists of the keyword local, followed by a sequence of definitions grouped with ( and ), followed by an expression.


tabular21802

Figure: Scheme definitions


The keyword local distinguishes this new class of expressions from other expressions, just as cond distinguishes conditional expressions from applications. The parenthesized sequence that follows local is referred to as the LOCAL DEFINITION .The definitions are called the LOCALLY DEFINED variables, functions, or structures. All those in the Definitions window are called TOP-LEVEL DEFINITIONS. Each name may occur at most once on the left-hand side, be it in a variable definition or a function definition. The expression in each definition is called the RIGHT-HAND SIDE expression. The expression that follows the definitions is the BODY.

Let us take a look at an example:

(local ((define (f x) (+ x 5))
        (define (g alon) 
          (cond 
            [(empty? alon) empty] 
            [else (cons (f (first alon)) (g (rest alon)))]))) 
  (g (list 1 2 3))) 
The locally defined functions are f and g. The right-hand side of the first function definition is (+ x 5); the second one is
(cond
  [(empty? alon) empty] 
  [else (cons (f (first alon)) (g (rest alon)))]) 
Finally, the body of the local-expression is (g (list 1 2 3)).


Exercises Exercise 18.1.1

Circle the locally defined variables and functions in red, the right-hand sides in green, and the body of the following local-expression in blue:

1. (local ((define x (* y 3)))
         (* x x)) 

2. (local ((define (odd an)
                 (cond 
                   [(zero? an) false] 
                   [else (even (sub1 an))])) 
               (define (even an) 
                 (cond 
                   [(zero? an) true] 
                   [else (odd (sub1 an))]))) 
         (even a-nat-num)) 

3. (local ((define (f x) (g x (+ x 1)))
               (define (g x y) (f (+ x y)))) 
         (+ (f 10) (g 10 20))) 
Solution

Exercise 18.1.2

The following phrases are not syntactically legal:

1. (local ((define x 10)
               (y (+ x x))) 
         y) 

2. (local ((define (f x) (+ (* x x) (* 3 x) 15))
               (define x 100) 
               (define f@100 (f x))) 
         f@100 x) 

3. (local ((define (f x) (+ (* x x) (* 3 x) 14))
               (define x 100) 
               (define f (f x))) 
         f) 
Explain why! Solution

Exercise 18.1.3

Determine which of the following definitions or expressions is legal and which one is not:

1. (define A-CONSTANT
         (not 
           (local ((define (odd an) 
                     (cond 
                       [(= an 0) false] 
                       [else (even (- an 1))])) 
                   (define (even an) 
                     (cond 
                       [(= an 0) true] 
                       [else (odd (- an 1))]))) 
             (even a-nat-num)))) 

2. (+ (local ((define (f x) (+ (* x x) (* 3 x) 15))
                  (define x 100) 
                  (define f@100 (f x))) 
            f@100) 
          1000) 

3. (local ((define CONST 100)
               (define f x (+ x CONST))) 
         (define (g x y z) (f (+ x (* y z))))) 
Explain why each expression is legal or illegal. Solution


[previous] [up] [next]     [index]
Next: Semantics of local Up: Organizing Programs with local Previous: Organizing Programs with local

PLT