Functional Problem Solving (CSC 151 2014F) : EBoards

CSC151.01 2014F, Class 20: Anonymous Procedures


Overview

Preliminaries

Admin

Upcoming Work

Quiz Topics

Reminder: You may bring a sheet of notes to Friday's quiz.

Extra Credit Opportunities

Academic

Peer Support

Questions (Administrative, Recent Topics, Etc.)

Followup Notes from Tuesday

Two ways to write irgb-brighter?

(define irgb-brighter?
  (lambda (color1 color2)
    (if (> (rgb-brightness color1) (rgb-brightness color2))
        #t
        #f)))

vs.

(define irgb-brighter?
  (lambda (color1 color2)
    (> (rgb-brightness color1) (rgb-brightness color2))))

Multiple ways to write irgb-4grey with if?

(define irgb-4grey
  (lambda (color)
    (if (< (irgb-brightness color) 0.25)
        grey1
        (if (and (< (irgb-brightness color) 0.5)
                 (>= (irgb-brightness color) 0.25))
             grey2
             (if (and (< (irgb-brightness color) 0.75)
                      (>= (irgb-brightness color) 0.5))
                  grey3
                  (if (and (<= (irb-brightness color 1.0))
                           (>= (irg-brigthness color 0.75)))
                       grey4
                       0))))))

vs.

(define irgb-4grey
  (lambda (color)
    (if (< (irgb-brightness color) 0.25)
        grey1
        (if (and (< (irgb-brightness color) 0.5)
                 (>= (irgb-brightness color) 0.25))
             grey2
             (if (and (< (irgb-brightness color) 0.75)
                      (>= (irgb-brightness color) 0.5))
                  grey3
                  grey4)))))

vs.

(define irgb-4grey
  (lambda (color)
    (if (<= (irgb-brightness color) 0.25)
        grey1
        (if (<= (irgb-brightness color) 0.5)
            grey2
            (if (<= (irgb-brightness color) 0.75)
                grey3
                grey4)))))

vs.

(define brightness-4grey
  (lambda (brightness)
    (if (<= brightness 0.25)
        grey1
        (if (<= brightness 0.5)
            grey2
            (if (<= brightness  0.75)
                grey3
                grey4)))))
(define color-4grey
  (lambda (color)
    (brightness-4grey (irgb-brightness (color->irgb color)))))

vs.

(define irgb-4grey
  (lambda (color)
    (if (<= 0 (irgb->brightness color) 0.25)
        grey1
        (if (<= 0.25 (irgb-brightness color) 0.5)
            grey2
            (if (<= 0.5 (irgb-brightness color) 0.75)
                grey3
                grey4)))))

Followup to HW 4

(define apply-transform
  (lambda (transform irgb-color)
    (transform irgb-color)))

(define visualize-transforms
  (lambda (irgb-color list-of-transforms number-of-transforms)
    (visualize-colors (map apply-transform
                           list-of-transforms
                           (make-list number-of-transforms irgb-color))
                      number-of-transforms)))

vs.

(define visualize-transforms
  (lambda (irgb-color list-of-transforms number-of-transforms)
    (visualize-colors (map (lambda (trans) (trans irgb-color))
                           list-of-transforms)
                      number-of-transforms)))

Your Questions on the Reading

What's the point of l-s and r-s?

Lets you write shorter (and more readable?) code

    (map (l-s * 2) (iota 5))
    (map (lambda (x) (* 2 x)) (iota 5))

What do left section and right section do?

Fill in one of the parameters for a two-parameter procedure.

left-section fills in the first parameter. right-section fills in the second

    > (map (l-s / 2) (iota 5))
    ; (map (lambda (val) (/ 2 val)) (iota 5))
    ; (list (/ 2 0) (/ 2 1) (/ 2 2) (/ 2 3) (/ 2 4))

    > (map (r-s / 2) (iota 5))
    ; (map (lambda (val) (/ val 2)) (iota 5))
    (list (/ 0 2) (/ 1 2) (/ 2 2) (/ 3 2) (/ 4 2))

_Can you explain this weird code? (map (o increment (l-s * 2) (r-s mod 7)) (iota 50))

Start with

    > (map (o (r-s mod 7)) (iota 20))
    ; (map (lambda (x) (mod x 7)) '(0 ... 19))
    > (map (o (l-s * 2) (r-s mod 7)) (iota 20))
    ; (map (l-s * 2) '(0 1 2 3 4 5 6 0 1 2 3 4 5 6 ...))
    0 2 4 8 10 12 0 2 4 8 10

    > (map (o (r-s mod 7)) (iota 20))
    '(0 1 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3 4 5)
    > (map (o (l-s * 2) (r-s mod 7)) (iota 20))
    '(0 2 4 6 8 10 12 0 2 4 6 8 10 12 0 2 4 6 8 10)
    > (map (o (l-s mod 7)) (iota 20))
    . . share/lib/gigls/higher.rkt:256:20: modulo: undefined for 0
    > (map (o (l-s mod 7)) (drop (iota 20) 1))
    '(0 1 1 3 2 1 0 7 7 7 7 7 7 7 7 7 7 7 7)
    > (map (o increment (l-s * 2) (r-s mod 7)) (iota 20))
    '(1 3 5 7 9 11 13 1 3 5 7 9 11 13 1 3 5 7 9 11)

My Questions on the Reading

Discuss each of these with your partner and be prepared to answer.

a. What is the difference between (l-s + 2) and (r-s + 2)?

b. What is the difference between (l-s - 2) and (r-s - 2)?

c. What is the difference between (o increment sqrt) and (o sqrt increment)?

Lab

For the extras, we want things like

    ; Compose f and g.  Return the function that, given an input
    ; x, applies g to x and then f to x.
    ; In math notation, (fog)(x) = f(g(x))
    (define my-compose
      (lambda (f g)
        (...)))