Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: removed erroneous braces in the double-num and <3-nums exercise

...

Code Block
max-num: non-empty-list -> number
(define (max-num a-nelon)
  (local [(define max-rest (max-num (rest a-nelon)))]
    (cond [(empty? (rest a-nelon)) (first a-nelon)]
          [else  (if (<= (first a-nelon) max-rest) 
                     max-rest
                     (first a-nelon))])))

...

Make sure you don't put a local too early in your code.

Code Block


max-num: non-empty-list -> number
(define (max-num a-nelon)
  (cond [(empty? (rest a-nelon)) (first a-nelon)]
        [else  (local [(define max-rest (max-num (rest a-nelon)))
                       (define first-smaller? (<=(first a-nelon) max-rest))]
                  (if first-smaller? max-rest (first a-nelon)))]))

...

Code Block
(define x 5)
(define y 5)
(define z 10)
(define x_0 7) 
(define y_0 x_0)
(+ x_0 y_0 z)

...

Code Block
(define x 5)
(define y 5)
(define z 10)
(define x_0 7) 
(define y_0 7)
(+ x_0 y_0 z)

...

Code Block
(define x 5)
(define y 5)
(define z 10)
(define x_0 7) 
(define y_0 7)
(+ 7 y_0 z)

...

Code Block
(define x 5)
(define y 5)
(define z 10)
(define x_0 7) 
(define y_0 7)
(+ 7 7 z)

=>

Code Block
(define x 5)
(define y 5)
(define z 10)
(define x_0 7) 
(define y_0 7)
(+ 7 7 10)

...

Code Block
(define x 5)
(define y 5)
(define z 10)
(define x_0 7) 
(define y_0 7)
24

Note: {+} is a function of an arbitrary number of numbers in Scheme. Hence (+ 7 7 10) reduces to 24 in a single step. The longer form of this evaluation adds an extra step in lifiting the local.

...

Code Block
;; filter : (X -> boolean) (list-of X) -> (list-of X) 
;; Purpose: (filer p alox) returns the elements e of alox for which (p e) is true

(define (filter keep? alox)
  (cond [(empty? alox) empty] 
        [(cons? alox) (if (keep? (first alox))         
                       (cons (first alox) (filter keep? (rest alox)))
                       (filter keep? (rest alox)))]))

We can putatively improve the preceding definition by eliminating the repetition of the code (filter keep? (rest alox)):

Code Block

;; filter : (X -> boolean) (list-of X) -> (list-of X) 
;; (filer p alox) returns the elements e of alox for which (p e) is true

(define (filter keep? alox)         
  (cond [(empty? alox) empty] 
        [(cons? alox) 
         (local [(define kept-rest (filter keep? (rest alox)))]
           (if (keep? (first alox))         
               (cons (first alox) kept-rest)
               kept-rest))]))         

The refactoring of the first definition of filter given above is borderline. It does not improve the running time of the program since the duplicate calls appear in disjoint control paths. Which of the two preceding definitions is easier to understand? The answer is debatable; it is not clear that the "improved" definition is better.

...

Code Block
(define (Filter keep? alox) 
  (local [(define (filter-help alox) ...]  
    (filter-help alox)))              

Do not bother writing a contract, purpose statement, ortemplate instantiation for this function since we recommend that you throw this code away after the lab is over. The name has been changed to Filter} to avoid colliding with the Scheme library function named {{filter.

...

Code Block
;; double-nums : (list-of number) -> (list-of number)
;; (double-num (list e1 ... ek)) returns (list d1 ... dk) where each element di is twice ei.
      
(define (double-nums alon)          
  (cond [(empty? alon) empty]
        [(cons? alon)  (cons (* 2 (first alon)) (double-nums (rest alon)))]))

     
;; <3-nums : (list-of number) -> (list-of boolean) 
;; (<3-nums (list e1 ... en)) returns (list b1 ... bn) where bi has the value (< ei 3)
  
(define (<3-nums alon)          
  (cond [(empty? alon) empty]
        [(cons? alon)  (cons (< (first alon) 3) (<3-nums (rest alon))))]))

...

Exercises.
  • Write double-nums ]] and {{ <3-nums using map.

The Big Picture

...