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

Pragmatics of local, Part 1

The most important use of local-expressions is to ENCAPSULATE a collection of functions that serve one purpose. Consider for an example the definitions for our sort function from section [cross-reference]:

;; sort : list-of-numbers -> list-of-numbers
(define (sort alon) 
  (cond 
    [(empty? alon) empty] 
    [(cons? alon) (insert (first alon) (sort (rest alon)))]))

;; insert : number list-of-numbers (sorted) -> list-of-numbers (define (insert an alon) (cond [(empty? alon) (list an)] [else (cond [(> an (first alon)) (cons an alon)] [else (cons (first alon) (insert an (rest alon)))])]))

The first definition defines sort per se, and the second one defines an auxiliary function that inserts a number into a sorted list of numbers. The first one uses the second one to construct the result from a natural recursion, a sorted version of the rest of the list, and the first item.

The two functions together form the program that sorts a list of numbers. To indicate this intimate relationship between the functions, we can, and should, use a local-expression. Specifically, we define a program sort that immediately introduces the two functions as auxiliary definitions:

;; sort : list-of-numbers -> list-of-numbers
(define (sort alon) 
  (local ((define (sort alon) 
            (cond 
              [(empty? alon) empty] 
              [(cons? alon) (insert (first alon) 
                                    (sort (rest alon)))])) 
          (define (insert an alon) 
            (cond 
              [(empty? alon) (list an)] 
              [else (cond 
                      [(> an (first alon)) (cons an alon)] 
                      [else (cons (first alon) 
                                  (insert an (rest alon)))])]))) 
    (sort alon))) 
Here the body of local-expressions simply passes on the argument to the locally defined function sort.

Guideline on the Use of local

Develop a function following the design recipes. If the function requires the use of auxiliary definitions, group them in a local-expression and put the local-expression into a new function definition. The body of the local should apply the main function to the arguments of the newly defined function.


Exercises

Exercise 18.1.6

Evaluate (sort (list 2 1 3)) by hand until the locally defined sort function is used. Do the same for (equal? (sort (list 1)) (sort (list 2)))Solution

Exercise 18.1.7

Use a local expression to organize the functions for moving pictures from section [cross-reference]Solution

Exercise 18.1.8

Use a local expression to organize the functions for drawing a polygon in figure [cross-reference]Solution

Exercise 18.1.9

Use a local expression to organize the functions for rearranging words from section [cross-reference]Solution

Exercise 18.1.10

Use a local expression to organize the functions for finding blue-eyed descendants from section [cross-reference]Solution



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

PLT