Lecture 17
On Functional Programming
Logistics and Week Ahead
Today
For some - Ethics Module 7 - Labor
Announcements
The story up until now
The story up until now
Rules of computation in Racket
Look at the expression
Special cases: Rules of computation in Racket
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)
If it starts with if:
(if test consequent alternative)
Special cases: Rules of computation in Racket
If it starts with define-struct
(define-struct typename (properties…))
If it starts with cond:
(cond [test1 result1]
[ … ]
[else resultlast])
If it starts with require:
(require some-library)
If it starts with check-expect:
(check-expect thing-1 thing-2)
Simple Execution Examples
2
"Friday"
#true
Simple Execution Examples
(define a "FRIDAY!")
a
map
What are these?
(λ (x)
(+ x 2))
(define pizza "connor")
Compound Examples
(define pizza "connor")
(cond [(string=? pizza "pizza") #false ]
[(string=? pizza "pineapple") #true ]
[(= pizza 5) "wait" ]
[else "uh oh"])
((λ (x)
(+ x 2))
5)
(define (map-helper func in out)
(if (empty? in)
out
(loop func
(rest in)
(cons (func (first in))
out))))
(define (map lst)
(map-helper - lst '())
map
Pernicious Examples
(foldl or #false '(#false #true #false #false))
; my-filter/iter-helper : (any -> bool) (listof any) (listof any) -> (listof any)
; helper function for my-filter/iter
(define (my-filter/iter-helper pred l accum)
(cond [(empty? l) accum]
[(pred (first l)) (my-filter/iter-helper pred
(rest l)
accum)]
[else (my-filter/iter-helper pred
(rest l)
accum)]))
; my-filter/iter : (any -> bool) (listof any) -> (listof any)
; Works identically to filter, but uses iterative recursion
(define (my-filter/iter pred lst)
(reverse (my-filter/iter-helper pred lst '())))
(my-filter/iter even? '(1 2 3 4))
; my-filter/iter-helper : (any -> bool) (listof any) (listof any) -> (listof any)
; helper function for my-filter/iter
(define (my-filter/iter-helper pred l accum)
(cond [(empty? l) accum]
[(pred (first l)) (my-filter/iter-helper pred
(rest l)
accum)]
[else (my-filter/iter-helper pred
(rest l)
accum)]))
; my-filter/iter-helper : (any -> bool) (listof any) (listof any) -> (listof any)
; helper function for my-filter/iter
(define (my-filter/iter-helper pred l accum)
(cond [(empty? l) accum]
[(pred (first l)) (my-filter/iter-helper pred
(rest l)
(cons (first l) accum))]
[else (my-filter/iter-helper pred
(rest l)
accum)]))
; my-iterated-overlay/iter : (num -> img) num -> img
; Works the same was as iterated-overlay using iterative recursion.
(define (my-iterated-overlay/iter gen num)
(local [(define (helper mygen n acc)
(local [(define recursive-step n)]
(cond [(= n 0) acc]
[else (helper mygen
recursive-step
(overlay (mygen recursive-step)
acc))])))]
(helper gen num empty-image)))
(my-iterated-overlay/iter (lambda (x) (square (* x 50) "outline" "red")) 5)
; my-iterated-overlay/iter : (num -> img) num -> img
; Works the same was as iterated-overlay using iterative recursion.
(define (my-iterated-overlay/iter gen num)
(local [(define (helper mygen n acc)
(local [(define recursive-step (- n 1))]
(cond [(= n 0) acc]
[else (helper mygen
recursive-step
(overlay (mygen recursive-step)
acc))])))]
(helper gen num empty-image)))
(my-iterated-overlay/iter (lambda (x) (square (* x 50) "outline" "red")) 5)
;; A binary-tree is one of:
;; - A string (a name), e.g. "Connor"
;; - A (make-branch string[s] binary-tree binary-tree)
(define-struct branch (name left right))
;; Valid Trees given this definition!
“Connor”
(make-branch “Rex” “Buzz” “Stinky Pete”)
;; Invalid Trees given this definition!
(make-branch “Rex” #false #false)
(make-branch “Rex”)
Why though?
Functional vs Imperative Programming
A Little Game Design
Believe it or not, we have all the tools necessary to implement a real game!
And that's what we're going to do in Exercise 6!
Basics of Game Design: Model View Controller
A way of abstracting game design into three interconnected parts:
Model
View
Controller
Let's focus on the DATA behind the game
What sorts of data do we need to represent the game?
What objects exist?
What properties do they have?
How might we make these in Racket?
Let's focus on the REPRESENTATION of the game
How is each object represented visually?
How might we make these in Racket?
Let's focus on the LOGIC of the game
How does the game work?
How are objects manipulated?
How might we write this in Racket?
Basics of Game Design: Model View Controller
A way of abstracting game design into three interconnected parts:
Model
View
Controller