Functional Problem Solving (CSC 151 2014F) : EBoards
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] - [FAQ] [Teaching & Learning] [Grading] [Rubric] - [Calendar]
Current: [Assignment] [EBoard] [Lab] [Outline] [Reading]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines] [Readings]
Reference: [Setup] [VM] [Errors] - [Functions A-Z] [Functions By Topic] - [Racket] [Scheme Report (R5RS)] [R6RS] [TSPL4]
Related Courses: [Davis (2013F)] [Rebelsky (2014S)] [Weinman (2014F)]
Misc: [Submit Questions] - [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] - [Issue Tracker (Course)]
Overview
Reminder: You may bring a sheet of notes to Friday's quiz.
list, make-list, and iota)mapimage-select-rectangle!)compose, o, l-s, r-s (the latter two from today's class)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)))))
(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)))
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)
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)?
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)
(...)))