Tuesday, 1 January 2013

Exercise 190: Evaluate (squared>? 3 10), (squared>? 4 10), and (squared>? 5 10) by hand


Exercise 190: Evaluate (squared>? 3 10)(squared>? 4 10), and (squared>? 510) by hand. Then show that
(extract squared>? (list 3 4 5) 10)
evaluates to (list 4 5).
; Number Number -> Boolean
; is the area of a square with side x larger than c
(define (squared>? x c)
  (> (* x x) c))

(squared>? 3 10)

(squared>? 3 10)
=
 (> (* 3 3) 10)
=
 (> 9 10)
= false

(squared>? 4 10)

(squared>? 4 10)
=
 (> (* 4 4) 10)
=
 (> 16 10)
= true


(squared>? 5 10)

(squared>? 5 10)
=
 (> (* 5 5) 10)
=
 (> 15 10)
= true

Part Two


Show that (extract squared>? (list 3 4 5) 10) evaluates to (list 4 5)

This exercise I found very long winded but it was very useful to step through and see how the recursion built up the final result. Even though I am comfortable using recursion, it's not often you step through it like this.

(extract squared>? (list 3 4 5) 10)
=
(cond
  [(empty? (list 3 4 5)) empty]
  [else (cond
          [(squared>? (first (list 3 4 5) 10))
           (cons (first (list 3 4 5)
                 (extract squared>? (rest (list 3 4 5)) 10)))]
          [else (extract squared>? (rest (list 3 4 5)) 10)])])

=
(cond
  [false empty]
  [else (cond
          [(squared>? (first (list 3 4 5) 10))
           (cons (first (list 3 4 5)
                 (extract squared>? (rest (list 3 4 5)) 10)))]
          [else (extract squared>? (rest (list 3 4 5)) 10)])])
=
  (cond
          [(squared>? (first (list 3 4 5) 10))
           (cons (first (list 3 4 5)
                 (extract squared>? (rest (list 3 4 5)) 10)))]
          [else (extract squared>? (rest (list 3 4 5)) 10)])
=
  (cond
          [(squared>? 3 10)
           (cons (first (list 3 4 5)
                 (extract squared>? (rest (list 3 4 5)) 10)))]
          [else (extract squared>? (rest (list 3 4 5)) 10)])
=
   (cond
          [false
           (cons (first (list 3 4 5)
                 (extract squared>? (rest (list 3 4 5)) 10)))]
          [else (extract squared>? (rest (list 3 4 5)) 10)])

=
  (extract squared>? (rest (list 3 4 5)) 10)

=
  ; time to recurse
  (extract squared>? (list 4 5) 10)
=
    (cond
      [(empty? (list  4 5)) empty]
      [else (cond
              [(squared>? (first (list 4 5) 10))
               (cons (first (list 4 5)
                            (extract squared>? (rest (list 4 5)) 10)))]
              [else (extract squared>? (rest (list 4 5)) 10)])])
=
    (cond
      [false empty]
      [else (cond
              [(squared>? (first (list 4 5) 10))
               (cons (first (list 4 5)
                            (extract squared>? (rest (list 4 5)) 10)))]
              [else (extract squared>? (rest (list 4 5)) 10)])])
=
    (cond
      [(squared>? 4 10))
       (cons (first (list 4 5)
                    (extract squared>? (rest (list 4 5)) 10)))]
      [else (extract squared>? (rest (list 4 5)) 10)])])  

=
    (cond
      [true
       (cons (first (list 4 5)
                    (extract squared>? (rest (list 4 5)) 10)))]
      [else (extract squared>? (rest (list 4 5)) 10)])])  

=

       (cons (first (list 4 5)
                    (extract squared>? (rest (list 4 5)) 10)))
     
=
      (cons  4  (extract squared>? (rest (list 4 5)) 10)))
; recurse again.  
=
      (cons  4  (extract squared>? (list  5)) 10)

=
      (cons  4
             (cond
               [empty? (list 5) empty]
               [else (cond
                       [(squared>? (first (list  5) 10))
                        (cons (first (list  5)
                                     (extract squared>? (rest (list  5)) 10)))]
                       [else (extract squared>? (rest (list  5)) 10)])]))

=
      (cons  4
             (cond
               [false empty]
               [else (cond
                       [(squared>? (first (list  5) 10))
                        (cons (first (list  5)
                                     (extract squared>? (rest (list  5)) 10)))]
                       [else (extract squared>? (rest (list  5)) 10)])]))


=
      (cons  4
             (cond
               [(squared>? (first (list  5) 10))
                (cons (first (list  5)
                             (extract squared>? (rest (list  5)) 10)))]
               [else (extract squared>? (rest (list  5)) 10)]))

=
      (cons  4
             (cond
               [(squared>? (  5 10))
                (cons (first (list  5)
                             (extract squared>? (rest (list  5)) 10)))]
               [else (extract squared>? (rest (list  5)) 10)]))

=
      (cons  4
             (cond
               [true
                (cons (first (list  5)
                             (extract squared>? (rest (list  5)) 10)))]
               [else (extract squared>? (rest (list  5)) 10)]))

=
      (cons  4
             (cons (first (list  5)
                          (extract squared>? (rest (list  5)) 10))))
             
=
      (cons  4
             (cons  5
                    (extract squared>? (rest (list  5)) 10)))

=
      (cons  4
             (cons  5
                    (extract squared>? empty 10)))

; recurse again
=
    (cons  4
           (cons  5
                  (cond
                    [empty? empty] empty)
                  [else (cond
                          [(squared>? (first empty 10))
                           (cons (first empty
                                        (extract squared>? (rest empty) 10)))]
                          [else (extract squared>? (rest empty) 10)])])))

=
      (cons  4
             (cons  5
                    (cond
               [true empty]
               [else (cond
                       [(squared>? (first empty 10))
                        (cons (first empty
                                     (extract squared>? (rest empty) 10)))]
                       [else (extract squared>? (rest empty) 10)])])))


=
      (cons  4
             (cons  5
                    empty))

= (list 4 5)
             

No comments:

Post a Comment