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
(ceiling -9.3)
-> -9.0
.What do we know about ordering in Scheme?
(sqrt (+ (square (- x0 x1)) (square (- y0 y1))))
This seems to compute the distance between two points.
Are there other ways we can control sequencing (other than inside-out)?
Concrete: Suppose we wanted to be absolutely sure that we subtracted y0 from y1 first.
(define difference-of-ys (- y0 y1)) (define square-of-difference-of-ys (square difference-of-ys))
Another idea (good idea, if "innermost first" is correct, but the real policy is "evaluate arguments before applying function")
(sqrt (+ (square (- x0 x1)) (square (+ 0 (- y0 y1)))))
What do you expect to get from this?
(define val 16)
val
(sqrt val)
val
Conclusion: sqrt
does not change val
.
What do you expect go get from this?
(define drawing (scale-drawing 40 drawing-unit-circle))
(define d2 (recolor-drawing "red" drawing))
(image-show (drawing->image drawing 100 100))
Discovery: Same thing. The recolor-drawing
does not change
drawing
.
Basic idea:
Meaning: "When called, substitue for parameters, and evaluate the chunk of code."
(define enlarge-and-recolor (lambda (drawing) (recolor-drawing "red" (scale-drawing 10 drawing))))
e.g.,
(enlarge-and-recolor drawing-unit-circle)
If there are multiple expressions, we only get the value of the last one. The following will scale, not recolor.
(define enlarge-and-recolor
(lambda (drawing)
(recolor-drawing "green" drawing)
(scale-drawing 10 drawing)))