Lecture 3 (Pre-Recorded)
Compound Functions
Rules of computation in Racket
How do we make a square?
How do we make a row of squares?
How do we make a grid?
Wow, that’s a lot of typing…
(above (beside (square 50 "outline" "black")
(square 50 "outline" "black")
(square 50 "outline" "black")
(square 50 "outline" "black"))
(beside (square 50 "outline" "black")
(square 50 "outline" "black")
(square 50 "outline" "black")
(square 50 "outline" "black"))
(beside (square 50 "outline" "black")
(square 50 "outline" "black")
(square 50 "outline" "black")
(square 50 "outline" "black"))
(beside (square 50 "outline" "black")
(square 50 "outline" "black")
(square 50 "outline" "black")
(square 50 "outline" "black")))
Wow, that’s a lot of typing…
(above (beside (square 50 "outline" "black")
(square 50 "outline" "black")
(square 50 "outline" "black")
(square 50 "outline" "black"))
(beside (square 50 "outline" "black")
(square 50 "outline" "black")
(square 50 "outline" "black")
(square 50 "outline" "black"))
(beside (square 50 "outline" "black")
(square 50 "outline" "black")
(square 50 "outline" "black")
(square 50 "outline" "black"))
(beside (square 50 "outline" "black")
(square 50 "outline" "black")
(square 50 "outline" "black")
(square 50 "outline" "black")))
Why isn't there some way of telling Racket: "whenever I say quack I really mean 'draw a square in outline mode that's black and is size 50'"...
defining new names
Defining new names
(define name value)
Simplifying with names
(define unit �(square 50 "outline" "black"))
(above (beside unit unit unit unit)
(beside unit unit unit unit)
(beside unit unit unit unit)
(beside unit unit unit unit))
define isn’t a function
Why?
Rules of computation in Racket
Special forms
This...still isn't that nice to write.
(define unit �(square 50 "outline" "black"))
(above (beside unit unit unit unit)
(beside unit unit unit unit)
(beside unit unit unit unit)
(beside unit unit unit unit))
How can we do better?
(Don’t say “loop”; we won’t get to those until next time
Name the row
(define unit� (square 50 "outline" "black"))
(define row (beside unit unit unit unit))
(above row row row row)
Your client called
Now they also want one with circles rather than squares
New and improved
(define circ-unit� (circle 25 � "outline" � "black"))
(define circ-row� (beside circ-unit circ-unit� circ-unit circ-unit))
(above circ-row circ-row � circ-row circ-row)
Your client called
Now they want a grid of grids of circles rather than squares
What’s wrong with this?
What we want to be able to say
(grid (circle 25 "outline" "black"))
What kind of a thing is grid?
lambda expressions
Compound functions
(lambda (name1 name2 … namen) output-expression)
(λ (name1 name2 … namen) output-expression)
Remember, functions are just another data object!
You can construct new functions from old code using λ expressions
Note: you type the λ symbol by typing command-\ on Macs or control-\ on Windows
Compound functions
(lambda (name1 name2 … namen) output-expression)
(λ (name1 name2 … namen) output-expression)
Remember, functions are just another data object!
When called, the function
Compound functions
(lambda (name1 name2 … namen) output-expression)
(λ (name1 name2 … namen) output-expression)
If you call (λ (x) (+ x 1)) with an input of 2
So ((λ (x) (+ x 1)) 2) is a function call
Defining grid
(define row� (λ (unit)� (beside unit unit unit unit)))
(define stack� (λ (unit)� (above unit unit unit unit)))
(define grid�(λ (unit)� (stack (row unit))))
Using / Running grid
(define row� (λ (unit)� (beside unit unit unit unit)))
(define stack� (λ (unit)� (above unit unit unit unit)))
(define grid�(λ (unit)� (stack (row unit))))
(grid (circle 25 "solid" "blue"))
Using / Running grid
(define row� (λ (unit)� (beside unit unit unit unit)))
(define stack� (λ (unit)� (above unit unit unit unit)))
(define grid�(λ (unit)� (stack (row unit))))
(grid (circle 25 "solid" "blue"))
notating�type signatures
Type signatures
Code comments
Notating type signatures in code
function name: input types … -> output type
square: number string color -> image
Notating type signatures in code
grid: image -> image
row: image -> image
stack: image -> image
Signature and purpose
; grid: image -> image�; Make a grid of identical pictures
(define grid� (λ (unit)� (stack (row unit))))
Defining grid
; grid: image -> image�; Make a grid of identical pictures
(define grid�(λ (unit)� (stack (row unit))))
; row: image -> image�; Make a row of identical pictures
(define row� (λ (unit)� (beside unit unit unit unit)))
; stack: image-> image�; Make a stack of identical pictures
(define stack� (λ (unit)� (above unit unit unit unit)))