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)]
For question 1 on the exam, what should I get for (red-dominates? (irgb 65 64 64)
#t
What is going to be on the quiz tomorrow?
cons, car, cdr, and null?
one-two, won-too, and want-to)image-compute!
let and let*
let and lambdaCan we go over let and let*?
Both
letandlet*are used to associate names with values (of expressions).Both have similar forms
(let ([NAME1 EXP1]
[NAME2 EXP2]
[NAME3 EXP3])
BODY)
(let* ([NAME1 EXP1]
[NAME2 EXP2]
[NAME3 EXP3])
BODY)
What happens if a latter expression references an earlier name?
(let* ([x 10]
[y 20]
[z (+ x y)])
(* z z))
900
(let ([x 10]
[y 20]
[z (+ x y)])
(* z z))
ERROR: x is unbound
Evaluation: For
let: Do all of the expressions, and then do the bindings. Forlet*, do them in sequence.Unexpected results
(define x 5)
(define y 3)
(let ([x 10]
[y 20]
[z (+ x y)])
(* z z))
Note: Mousing over the x and y with "check syntax" shows the relationships.
Looking at the code above, what's the value of x afterwards?
Anything defined within a
letis supposed to be "forgotten" outside of thelet. Soxwill be back to the 5 andzwill be undefined.
Why are there differences in interpretation?
let*does a strict sequence,letlets you distribute the work.
Why use let rather than let*?
Your code is likely to be faster. Make it a policy to use
letunless you need the behavior oflet*.
Can you do an example of using a lambda in a call to map?
Last year, the College gave all faculty a 2% raise plus $100 bonus raise. Given a list of salaries, let's compute the new salaries
(map (lambda (salary) (+ 100 (* salary 1.02))) (list 50000 20000 30000 89214))
Where does the lambda get its input from?
The
mapsupplies the input.
Shouldn't we use sectioning and composition to solve that problem?
Either way works.
(map (o (r-s + 100) (r-s * 1.02)) (list 50000 20000 30000 89214))
Can we use l-s in the previous?
Yes.
Can you tell me about caar?
(define caar (o car car))
No, I get that Sam. Think. Why would I ever want to take the car of
the first element of a list?
Sometimes we use lists for compound data (name class-year prospective-major)
We might have a list of such things
(define students (list (list "Syd" 2016 "Computer Science")
(list "Tracy" 2017 "Undeclared")
(list "Corey" 2015 "Independent Major: Peace")
(list "Andy" 2017 "Informatics")))
And taking the
caarwill give me the first name of the first student.
What is that weird single quote thing you just deleted?
Lists and procedure applications look incredibly similar. The single quote allows us to say "Take this verbatim and don't evaluate it"
Exam problem 5: Does it have to ensure that the parameter is a list of color names?
No.
Exam problem 3: Is efficiency or concision more important?
Efficiency.
Evaluate the following expressions:
(define lst1 (cons 'a (cons 'b 'c)))
(define lst2 (cons (list 1 2 3) lst1))
(define lst3 (cons "a" (cons "b" (cons "c" null))))
(define lst4 (append (list 1 2 3) lst1))
> (cadr lst1) ; b
> (cadr lst2) ; a
> (cadr lst4) ; 1
> (cadr lst3) ; "b"
> (cdar lst1) ; Error! 'a is not a list.
> (cdar lst2) ; '(2 3)
> (cdar lst3) ; Error!
Which of the following is a better implementation of "average-with", which averages each element of a list of irgb colors with a color (given by name)? And why? You may assume that irgb-average exists and averages two irgb-colors.
(define average-with
(lambda (list-of-colors color-name)
(map (lambda (color) (irgb-average color (color-name->irgb color-name)))
list-of-colors)))
(define average-with
(let ([extra (color-name->irgb color-name)])
(lambda (list-of-colors color-name)
(map (lambda (color) (irgb-average color extra))
list-of-colors))))
The second one is erroneous, so the first one is better.