Lecture 6 (Pre-Recorded)
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 (λ (picture-num)
(rotate (* 36 picture-num)
(rectangle 50 200
"solid" "red")))
5)
Further Parameterize
How do we vary the color?
(iterated-overlay (λ (picture-num)
(rotate (* 36 picture-num)
(rectangle 50 200
"solid" "red")))
5)
Further Parameterize
How do we vary the color?
(iterated-overlay (λ (picture-num)
(rotate (* 36 picture-num)
(rectangle 50 200
"solid"
(color (- 200 (* picture-num 50))
(* picture-num 50)
0))))
5)
Make it into a Function
How do we functionalize it?
(iterated-overlay (λ (picture-num)
(rotate (* 36 picture-num)
(rectangle 50 200
"solid"
(color (- 200 (* picture-num 50))
(* picture-num 50)
0))))
5)
Make it into a function
How do we functionalize it?
(define rotary (λ (num-spokes)
(iterated-overlay
(λ (picture-num)
(rotate (* (quotient 360 (* 2 num-spokes)) picture-num)
(rectangle 50 200
"solid"
(color (- 255 (* picture-num (quotient 255 num-spokes)))
(* picture-num (quotient 255 num-spokes))
0))))
num-spokes)))
Local variables
Another abstraction tool
We keep dividing the same thing…
;; rotary: number -> image
;; creates a rotary image with rectangles that change color
(define rotary (λ (num-spokes)
(iterated-overlay
(λ (picture-num)
(rotate (* (quotient 360 (* 2 num-spokes)) picture-num)
(rectangle 50 200
"solid"
(color (- 255
(* picture-num
(quotient 255
num-spokes)))
(* picture-num
(quotient 255
num-spokes))
0))))
num-spokes)))
Local names in Racket
(local [(define name1 value1)
(define name2 value2)
…
(define namen valuen)]�output-expression)
Local names in Racket
(local [(define name1 value1)
(define name2 value2)
…
(define namen valuen)]�output-expression)
Saving some redundant work
;; rotary: number -> image
;; creates a rotary image with rectangles that change color
(define rotary-2 (λ (num-spokes)
(iterated-overlay
(λ (picture-num)
(local [(define q (quotient 250 num-spokes))]
(rotate (* (quotient 360 (* 2 num-spokes)) picture-num)
(rectangle 50 200
"solid"
(color (- 255
(* picture-num
q))
(* picture-num
q)
0)))))
num-spokes)))
Rules of computation in ISL+
Look at the expression
Special cases: Rules of computation in ISL+
If it starts with define
(define name value-expression)
If it starts with λ or lambda:
(λ (name1 … namelast) result-expression)
If it starts with the the word local
(local [(define name1 value1) � …� (define namelast valuelast)]� result-expression)
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)))