Functional Problem Solving (CSC 151 2015S) : EBoards

CSC151.01 2015S, Review Session 6: Conditionals and Beyond


Overview

Admin

Your Questions

How does Scheme handle expressions that have anonymous procedures?

Simple

    ((lambda (x) (* x x)) 2)

This is essentially the same as

    (define square (lambda (x) (* x x)))
    (square 2)

Or

    (let ([square (lambda (x) (* x x))])
      (square 2))

In the second or third cases, square is bound to (lambda (x) (* x x)), and so in evaluating (square 2) it gives ((lambda (x) (* x x)) 2)

So now we are doing function application. Function application involves binding and evaluating. Scheme binds x to the value 2 and then evaluate (* x x).

To evaluate (* x x), Scheme looks up the bindings for *, x, and x. * is built in. x is bound to 2, so it knows to compute (* 2 2). That's 4.

Sam's weird attempt to show all of that.

(define value (lambda (thing) (display "\t") (display thing) (newline) thing))$a

((value square) (value 2)) # 2

((value square) (value 2)) # 2 4 ((value (lambda (x) (* x x))) (value 2)) # 2 4 ((value (lambda (x) (* x x))) (value (+ (value 3) (value 5)))) # 3 5 8 64 ((value (lambda (x) (* (value x) (value x)))) (value (+ (value 3) (value 5)))) # 3 5 8 8 8 64

Okay, now can you do that for section?

Sure.

    ((l-s / 3) 5)

Scheme evaluates each thing, hopefully finds that the first thing is a procedure, and then applies the procedure in the "normal" way.

    (define l-s
      (lambda (proc left)
        (lambda (right)
          (proc left right))))

    ((l-s / 3) 5)
    (((lambda (proc left) (lambda (right) (proc left right)))
      / 3)
     5)
    ; Bind proc to / and left to 3
    ((lambda (right) (proc left right)) 5)
    ; Bind right to 5 and
    (proc left right)a
    ; Look them up
    (/ 3 5)
    ; And we're done
    3/5

Can we try that with map?

    (map (l-s / 3) (list 1 2 3 4))
    ; We've seen that (l-s / 3) evaluates to something complicated
    ; (list 1 2 3 4) evaluates to '(1 2 3 4)
    ; map applies the function to each value in the list and combines
    ; them into a new list
    (list ((l-s / 3) 1) ((l-s / 3) 2) ((l-s / 3) 3) ((l-s / 3) 4))
    ; Each gets evaluated "simultaneously"
    ; The process above shows what happens
    (list 3/1 3/2 3/3 3/4)
    '(3 3/2 1 3/4)

How should I have solved #2?

How Sam thinks about things

Input list

    (t1 t2 t3 t4 t5)

Output list (goal)

    ((t1 c) (t2 c) (t3 c) (t4 c) (t5 c))

Solve with map

    (map (lambda (__) ___) list-of-transforms)

map will apply the function that we haven't written yet to each transform in list-of-transforms

    (map (lambda (transform) ___) list-of-transforms)

For each transformation, we want to apply it to the same color

    (map (lambda (transform) (transform irgb-color)) list-of-transforms)

For example

    (map (lambda (transform) (transform irgb-color))
         (list irgb-darker irgb-redder irgb-greener irgb-bluer))

    (list ((lambda (transform) (transform irgb-color) irgb-darker))
          ((lambda (transform) (transform irgb-color) irgb-redder))
          ((lambda (transform) (transform irgb-color) irgb-greener))
          ((lambda (transform) (transform irgb-color) irgb-bluer)))

    (list (irgb-darker irgb-color)
          (irgb-redder irgb-color)
          (irgb-greener irgb-color)
          (irgb-bluer irgb-color))

I'm trying to check my answer to one of the transforms.

    (define blend 
      (image-compute
       (lambda (col row)
         (irgb (* col 2)
               0
               (- 255 (* row 4))))
       257
       129))
    (map (lambda (row) (irgb->rgb-list (image-get-pixel blend 65 row)))
         (iota 129))

What Will the Quiz Look Like?

Two of four types of problems