Lecture 5
Lambda Abstraction
Rules of computation in ISL+
the substitution model
The substitution model
What is the value of this expression?
> ((λ (n) (+ n 1))
3)
What is the value of this expression?
> ((λ (n) (+ n 1)) 3)
> 4
Making a�color gradient
Drawing a line
Drawing a line
(rectangle 1 100 "solid" "blue")
Drawing a line
(rectangle 1 100 "solid" "blue")
Drawing a line
(rectangle 2 100 "solid" "blue")
Drawing a line
(rectangle 2 100 "solid" "blue")
Drawing a line
(rectangle 3 100 "solid" "blue")
Drawing a line
(rectangle 3 100 "solid" "blue")
Drawing lines "à la mode"
(λ (width)
(rectangle width 100 "solid" "blue"))
Even more customization
(λ (width)
(rectangle width 100 "solid" "blue"))
Even more customization
(λ (width)
(rectangle width
100
"solid"
(color 0
0
(- 255
(* width 10)))))
MORE LINES
(λ (width)
(rectangle width
100
"solid"
(color 0
0
(- 255
(* width 10)))))
MORE LINES
(iterated-beside (λ (width)
(rectangle width
100
"solid"
(color 0
0
(- 255
(* width 10)))))
10)
Reading the Code
(iterated-beside (λ (width)
(rectangle width
100
"solid"
(color 0
0
(- 255
(* width 10)))))
10)
calls the line generator
repeatedly and collects the results
generates a single line
number of lines to make
Stretching
(iterated-beside (λ (width)
(rectangle width
100
“solid”
(color 0
0
(- 255
(* width 10)))))
10)
Stretching
(iterated-beside (λ (width)
(rectangle (* 10 width)
100
“solid”
(color 0
0
(- 255
(* width 10)))))
10)
Changing the Palette
(iterated-beside (λ (width)
(rectangle (* 10 width)
100
“solid”
(color 0
0
(- 255
(* width 10)))))
10)
Changing the Palette
(iterated-beside (λ (width)
(rectangle (* 10 width)
100
“solid”
(color 0
(* width 25)
(- 255
(* width 25)))))
10)
Lambda abstraction
Functional abstraction
“Variablizing” expressions to generalize them
Abstracting expressions
(rectangle 1 100
"solid" "blue")
(rectangle width 100
"solid" "blue")
(λ (width)
(rectangle width
100
"solid"
"blue"))
Abstracting expressions
Alas, it’s often not that simple
(rectangle 1 100
"solid" "blue")
(rectangle width 100
"solid" "blue")
(λ (width)
(rectangle width 100
"solid" "blue"))
(λ (width)
(rectangle (* 10 width)
100
"solid"
"blue"))
Making a rotary pattern
How do we make a pattern of rectangles rotating around a central point?
Start simple
How do we make one rectangle?
Start simple
How do we make one rectangle?
(rectangle 50 200
"solid" "red")
Get slightly closer
How do we rotate one rectangle?
(rectangle 50 200
"solid" "red")
Get slightly closer
How do we rotate one rectangle?
(rotate 30
(rectangle 50 200
"solid" "red"))
Get slightly closer
How do we rotate one rectangle by an arbitrary angle?
(rotate 30
(rectangle 50 200
"solid" "red"))
Get slightly closer
How do we rotate one rectangle by an arbitrary angle?
(rotate degrees
(rectangle 50 200
"solid" "red"))
Make a function to do it!
How do we make a function to create arbitrary rotated rectangles?
(rotate degrees
(rectangle 50 200
"solid" "red"))
Make a function to do it!
How do we make a function to create arbitrary rotated rectangles?
(λ (degrees)
(rotate degrees
(rectangle 50 200
"solid" "red")))
Use the function you made
How do we make many rotated rectangles?
(λ (degrees)
(rotate degrees
(rectangle 50 200
"solid" "red")))
Use the function you made
How do we make many rotated rectangles?
(iterated-overlay (λ (degrees)
(rotate degrees
(rectangle 50 200
"solid" "red")))
50)
Use the function you made
How do we separate them more?
(iterated-overlay (λ (degrees)
(rotate degrees
(rectangle 50 200
"solid" "red")))
50)
Use the function you made
How do we separate them more?
(iterated-overlay (λ (degrees)
(rotate (* 36 degrees)
(rectangle 50 200
"solid" "red")))
5)
Further Parameterize
How do we separate them more?
(iterated-overlay (λ (current-count)
(rotate (* 36 current-count)
(rectangle 50 200
"solid" "red")))
5)
Further Parameterize
How do we vary the color?
(iterated-overlay (λ (current-count)
(rotate (* 36 current-count)
(rectangle 50 200
"solid" "red")))
5)
Further Parameterize
How do we vary the color?
(iterated-overlay (λ (current-count)
(rotate (* 36 current-count)
(rectangle 50 200
"solid"
(color (- 200 (* current-count 50))
(* current-count 50)
0))))
5)
Interpolating colors
(interpolate-colors color-1 color-2 frac)
Allows you to create colors between to colors specified by some frac.
Further Parameterize
How do we vary the color?
(iterated-overlay (λ (current-count)
(rotate (* 36 current-count)
(rectangle 50 200
"solid"
(interpolate-colors (color 255 0 0)
(color 0 255 0))
(/ current-count 4)))
5)
Further Parameterize
How do we vary the color?
(iterated-overlay (λ (current-count)
(rotate (* 36 current-count)
(rectangle 50 200
"solid"
(interpolate-colors (color 255 0 0)
(color 0 255 0))
(/ current-count 4)))
5)
Further Parameterize
How do we turn it into a function with any number of spokes?
(iterated-overlay (λ (current-count)
(rotate (* 36 current-count)
(rectangle 50 200
"solid"
(interpolate-colors (color 255 0 0)
(color 0 255 0))
(/ current-count 4)))
5)
Grids revisited
How do we make a row of items?
Making a row
;; row: image number -> image
;; Makes an row of copies of item
(define row (λ (item count)
(iterated-beside ;; number -> image
;; Just returns item
(λ (ignore) item)
count)))
Wait…can you do that?
;; row: image number -> image
;; Makes an row of copies of item
(define row (λ (item count)
(iterated-beside ;; number -> image
;; Just returns item
(λ (ignore) item)
count)))
But then why bother with a function (aka procedure)?
;; row: image number -> image
;; Makes an row of copies of item
(define row (λ (item count)
(iterated-beside item
count)))
That won’t work
How do we make a column?
Making a column
;; column: image number -> image
;; Makes an stack of copies of item
(define column (λ (item count)
(iterated-above (λ (ignore) item)
count))
How do we make a grid?
Making a grid
;; grid: image number number -> image
;; Makes an grid of copies of item
(define grid
(λ (item columns rows)
(column (row item columns)
rows)))
Take the item
;; grid: image number number -> image
;; Makes an grid of copies of item
(define grid
(λ (item columns rows)
(column (row item columns)
rows)))
Make a row of them
;; grid: image number number -> image
;; Makes an grid of copies of item
(define grid
(λ (item columns rows)
(column (row item columns)
rows)))
Make a stack of them
;; grid: image number number -> image
;; Makes an grid of copies of item
(define grid
(λ (item columns rows)
(column (row item columns)
rows)))
Make a function that does that
;; grid: image number number -> image
;; Makes an grid of copies of item
(define grid
(λ (item columns rows)
(column (row item columns)
rows)))
Name it grid
;; grid: image number number -> image
;; Makes an grid of copies of item
(define grid
(λ (item columns rows)
(column (row item columns)
rows)))