EECS 111 Lecture Notes - Lecture 18: Mutation, Foreach Loop

49 views9 pages
Cat$-type$name
Animal$-parent$type$name$
Because$the$parent$type$of$a$cat$is$an$animal,$they$have$a$name,$weight,$age
They$also$have$a$sleeping$spot
How$constructors$change$with$structs$with$parent$types
Predicate
(cat?$Pixel)$-->$true
(animal?$Pixel)$-->$true
Selectors$(pixel$is$the$name$of$the$cat)
(animal-name$pixel)$-->$parent$type$is$used$in$the$selector$for$fields$that$come$from$the$parent$type
(animal-weight$pixel)
(animal-age$pixel)
(cat-sleeping-spot$pixel)$-->$this$one$is$DIFFERENT
Mutators$(changing$selectors)
(set-cat-sleeping-spot!$Pixel$"bed")
(set-animal-weight!$Pixel$16)
When$making$a$type$name$with$parent$types,$make$sure$to$put$parent$types$first$
Ex.$If$a$dog's$parent$type$is$animal,$a$dog$is$(make-dog$string$number$number$string)
(define-struct$(dog$animal)$[best$friend])
Zoo-Weight$Procedure
Takes$a$list$of$animals$and$returns$the$sum$of$their$weights
(define$(zoo-weight$loa)
(cond$[(empty?$Loa)$$$$$$$0]
[else$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$(+$(animal-weight$(first$loa))
$$$$$(zoo-weight$(rest$loa)))]))
Mutators$with$zoo-weight$procedure
Make-animal-older!$
Takes$an$animal$as$input$and$returns$VOID$(the$point$of$this$procedure$is$the$side$effect)
Takes$an$animal$and$ages$that$animal$by$1$year$
Effect:$the$animal$is$one$year$older$
Exclamation$point$is$a$coding$standard$that$indicates$there$is$a$side$effect
(define$(make-animal-older!$A)
(define$pixel-age-before$(animal-age$pixel))$-->$pixel$ is$a$cat,$a$cat$is$an$animal,$animals$have$an$age$
You$CANT$say$cat-age$
§
Extracting$pixel's$age$
§
(check-expect*(begin*(make-animal-older!*pixel) --->$begin$executes$sequence$of$expressions$and$returns$value$of$the$last$one$
(animal-age*pixel))
(+*1*pixel-age-be fore ))
(define$(make-animal-older!$A)
$$$$$(set-animal-age!$A$(+$1$(animal-age$a))))
(begin$exp1
exp$2
exp$3
expn))
Grow-zoo!
Takes$a$list$of$animals,$and$adds$1$to$the$weight$of$each$animal
Effect:$each$animal$in$the$zoo$is$one$pound$heavier$
First,$define$a$list$of$animals$to$be$your$zoo$
(define$myzoo$(list$(make-cat$"buffer"$10$13$"lap")….include$more$animals!$
§
(define$zoo-weight-before$(zoo-weight$myzoo))$-->$capture$zoo$weight$before$by$using$PREVIOUSLY$defined$zooweight$proc$
(check-expect*(begin*(grow-zoo!*My*zoo)
*****(zoo-weight*myzoo))*-->$this$asks,$what$is$the$weight$now?
(+*(length*myzoo)*zoo-weight-before))* -->$weight$now$should$be$the$same$as$adding$the$length$of$my$zoo$to$the$weight$of$myzoo$before$
Foreach,)begin,)unless,)when)
(define$(grow-zoo!$loa)
(for-each$(lambda$(a)
(set-animal-weight! A$(+$1$(animal-weight$a))))--->$animal-weight$is$a$selector$that$allows$us$to$ask:$what$is$the$current$animal$weight?
$$$$$$$loa))
Error$Messages
Compute-grade:$takes$a$list$of$numbers$and$returns$a$number
Compute-grade:$computes$an$average$of$a$list$of$numbers$
We$want$to$make$sure$there$are$NO$invalid$inputs$
(define$(compute-grade$lst)
(local$[(define-sum$0)
(define$num-grades$(length$lst))
(define$(help)
$$$$(for-each$(lambda$(x)$---->$we$are$going$to$do$this$FOR$EACH$score$in$the$list$
$$$$$$$$$$$$$$(cond$[(or$(<$x$0)
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$(>$x$100))$$--->$if$our$score$is$an$impossible$score,$we$are$going$to$return$an$error$message
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$(error$x$"is$not$a$valid$grade.")]
$$$$$$$$$$$$$$$$$$$$$$$$$[else$(set!$Sum$(+$sum$x))]$-->$if$it$is$NOT$an$invalid$score,$add$the$score$to$sum$
Lst))]
$$$(help)))$----->$we$call$our$help$procedure$to$call$FOR$EACH$on$every$item$in$this$list,$and$add$it$to$the$sumBUT$we$can't$just$call$ help$
(begin$(help)
$$$$$$$$$$$$$(if$(>$num-grades$0)
$$$$$$$$$$$$$$$$$$(\ sum$num-grades)
$$$$$$$$$$$$$$$$$$(error$"there$were$no$grades$in$the$list."))))
If$you$want$to$check$these$error$messages:
(check-error$(compute-grade$empty)$$"there$were$no$grades$in$the$list.")
This$is$the$animal-weight$mutator,$which$allows$us$to$CHANGE$the$animal$weight$
Imperative*Programming:*Mutation*(cont.)
Friday,$March$2,$2018
12:06$PM
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 9 pages and 3 million more documents.

Already have an account? Log in
Cat$-type$name
Animal$-parent$type$name$
Because$the$parent$type$of$a$cat$is$an$animal,$they$have$a$name,$weight,$age
They$also$have$a$sleeping$spot
How$constructors$change$with$structs$with$parent$types
Predicate
(cat?$Pixel)$-->$true
§
(animal?$Pixel)$-->$true
§
Selectors$(pixel$is$the$name$of$the$cat)
(animal-name$pixel)$-->$parent$type$is$used$in$the$selector$for$fields$that$come$from$the$parent$type
§
(animal-weight$pixel)
§
(animal-age$pixel)
§
(cat-sleeping-spot$pixel)$-->$this$one$is$DIFFERENT
§
Mutators$(changing$selectors)
(set-cat-sleeping-spot!$Pixel$"bed")
§
(set-animal-weight!$Pixel$16)
§
When$making$a$type$name$with$parent$types,$make$sure$to$put$parent$types$first$
Ex.$If$a$dog's$parent$type$is$animal,$a$dog$is$(make-dog$string$number$number$string)
(define-struct$(dog$animal)$[best$friend])
Zoo-Weight$Procedure
Takes$a$list$of$animals$and$returns$the$sum$of$their$weights
(define$(zoo-weight$loa)
(cond$[(empty?$Loa)$$$$$$$0]
[else$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$(+$(animal-weight$(first$loa))
$$$$$(zoo-weight$(rest$loa)))]))
Mutators$with$zoo-weight$procedure
Make-animal-older!$
Takes$an$animal$as$input$and$returns$VOID$(the$point$of$this$procedure$is$the$side$effect)
Takes$an$animal$and$ages$that$animal$by$1$year$
Effect:$the$animal$is$one$year$older$
Exclamation$point$is$a$coding$standard$that$indicates$there$is$a$side$effect
(define$(make-animal-older!$A)
(define$pixel-age-before$(animal-age$pixel))$-->$pixel$ is$a$cat,$a$cat$is$an$animal,$animals$have$an$age$
You$CANT$say$cat-age$
§
Extracting$pixel's$age$
§
(check-expect*(begin*(make-animal-older!*pixel) --->$begin$executes$sequence$of$expressions$and$returns$value$of$the$last$one$
(animal-age*pixel))
(+*1*pixel-age-be fore ))
(define$(make-animal-older!$A)
$$$$$(set-animal-age!$A$(+$1$(animal-age$a))))
(begin$exp1
exp$2
exp$3
expn))
Grow-zoo!
Takes$a$list$of$animals,$and$adds$1$to$the$weight$of$each$animal
Effect:$each$animal$in$the$zoo$is$one$pound$heavier$
First,$define$a$list$of$animals$to$be$your$zoo$
(define$myzoo$(list$(make-cat$"buffer"$10$13$"lap")….include$more$animals!$
§
(define$zoo-weight-before$(zoo-weight$myzoo))$-->$capture$zoo$weight$before$by$using$PREVIOUSLY$defined$zooweight$proc$
(check-expect*(begin*(grow-zoo!*My*zoo)
*****(zoo-weight*myzoo))*-->$this$asks,$what$is$the$weight$now?
(+*(length*myzoo)*zoo-weight-before))* -->$weight$now$should$be$the$same$as$adding$the$length$of$my$zoo$to$the$weight$of$myzoo$before$
Foreach,)begin,)unless,)when)
(define$(grow-zoo!$loa)
(for-each$(lambda$(a)
(set-animal-weight! A$(+$1$(animal-weight$a))))--->$animal-weight$is$a$selector$that$allows$us$to$ask:$what$is$the$current$animal$weight?
$$$$$$$loa))
Error$Messages
Compute-grade:$takes$a$list$of$numbers$and$returns$a$number
Compute-grade:$computes$an$average$of$a$list$of$numbers$
We$want$to$make$sure$there$are$NO$invalid$inputs$
(define$(compute-grade$lst)
(local$[(define-sum$0)
(define$num-grades$(length$lst))
(define$(help)
$$$$(for-each$(lambda$(x)$---->$we$are$going$to$do$this$FOR$EACH$score$in$the$list$
$$$$$$$$$$$$$$(cond$[(or$(<$x$0)
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$(>$x$100))$$--->$if$our$score$is$an$impossible$score,$we$are$going$to$return$an$error$message
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$(error$x$"is$not$a$valid$grade.")]
$$$$$$$$$$$$$$$$$$$$$$$$$[else$(set!$Sum$(+$sum$x))]$-->$if$it$is$NOT$an$invalid$score,$add$the$score$to$sum$
Lst))]
$$$(help)))$----->$we$call$our$help$procedure$to$call$FOR$EACH$on$every$item$in$this$list,$and$add$it$to$the$sumBUT$we$can't$just$call$ help$
(begin$(help)
$$$$$$$$$$$$$(if$(>$num-grades$0)
$$$$$$$$$$$$$$$$$$(\ sum$num-grades)
$$$$$$$$$$$$$$$$$$(error$"there$were$no$grades$in$the$list."))))
If$you$want$to$check$these$error$messages:
(check-error$(compute-grade$empty)$$"there$were$no$grades$in$the$list.")
This$is$the$animal-weight$mutator,$which$allows$us$to$CHANGE$the$animal$weight$
Imperative*Programming:*Mutation*(cont.)
Friday,$March$2,$2018 12:06$PM
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 9 pages and 3 million more documents.

Already have an account? Log in
Cat$-type$name
Animal$-parent$type$name$
Because$the$parent$type$of$a$cat$is$an$animal,$they$have$a$name,$weight,$age
They$also$have$a$sleeping$spot
How$constructors$change$with$structs$with$parent$types
Predicate
(cat?$Pixel)$-->$true
§
(animal?$Pixel)$-->$true
§
Selectors$(pixel$is$the$name$of$the$cat)
(animal-name$pixel)$-->$parent$type$is$used$in$the$selector$for$fields$that$come$from$the$parent$type
§
(animal-weight$pixel)
§
(animal-age$pixel)
§
(cat-sleeping-spot$pixel)$-->$this$one$is$DIFFERENT
§
Mutators$(changing$selectors)
(set-cat-sleeping-spot!$Pixel$"bed")
§
(set-animal-weight!$Pixel$16)
§
When$making$a$type$name$with$parent$types,$make$sure$to$put$parent$types$first$
Ex.$If$a$dog's$parent$type$is$animal,$a$dog$is$(make-dog$string$number$number$string)
(define-struct$(dog$animal)$[best$friend])
Zoo-Weight$Procedure
Takes$a$list$of$animals$and$returns$the$sum$of$their$weights
(define$(zoo-weight$loa)
(cond$[(empty?$Loa)$$$$$$$0]
[else$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$(+$(animal-weight$(first$loa))
$$$$$(zoo-weight$(rest$loa)))]))
Mutators$with$zoo-weight$procedure
Make-animal-older!$
Takes$an$animal$as$input$and$returns$VOID$(the$point$of$this$procedure$is$the$side$effect)
Takes$an$animal$and$ages$that$animal$by$1$year$
Effect:$the$animal$is$one$year$older$
Exclamation$point$is$a$coding$standard$that$indicates$there$is$a$side$effect
(define$(make-animal-older!$A)
(define$pixel-age-before$(animal-age$pixel))$-->$pixel$ is$a$cat,$a$cat$is$an$animal,$animals$have$an$age$
You$CANT$say$cat-age$
§
Extracting$pixel's$age$
§
(check-expect*(begin*(make-animal-older!*pixel) --->$begin$executes$sequence$of$expressions$and$returns$value$of$the$last$one$
(animal-age*pixel))
(+*1*pixel-age-be fore ))
(define$(make-animal-older!$A)
$$$$$(set-animal-age!$A$(+$1$(animal-age$a))))
(begin$exp1
exp$2
exp$3
expn))
Grow-zoo!
Takes$a$list$of$animals,$and$adds$1$to$the$weight$of$each$animal
Effect:$each$animal$in$the$zoo$is$one$pound$heavier$
First,$define$a$list$of$animals$to$be$your$zoo$
(define$myzoo$(list$(make-cat$"buffer"$10$13$"lap")….include$more$animals!$
§
(define$zoo-weight-before$(zoo-weight$myzoo))$-->$capture$zoo$weight$before$by$using$PREVIOUSLY$defined$zooweight$proc$
(check-expect*(begin*(grow-zoo!*My*zoo)
*****(zoo-weight*myzoo))*-->$this$asks,$what$is$the$weight$now?
(+*(length*myzoo)*zoo-weight-before))* -->$weight$now$should$be$the$same$as$adding$the$length$of$my$zoo$to$the$weight$of$myzoo$before$
Foreach,)begin,)unless,)when)
(define$(grow-zoo!$loa)
(for-each$(lambda$(a)
(set-animal-weight! A$(+$1$(animal-weight$a))))--->$animal-weight$is$a$selector$that$allows$us$to$ask:$what$is$the$current$animal$weight?
$$$$$$$loa))
Error$Messages
Compute-grade:$takes$a$list$of$numbers$and$returns$a$number
Compute-grade:$computes$an$average$of$a$list$of$numbers$
We$want$to$make$sure$there$are$NO$invalid$inputs$
(define$(compute-grade$lst)
(local$[(define-sum$0)
(define$num-grades$(length$lst))
(define$(help)
$$$$(for-each$(lambda$(x)$---->$we$are$going$to$do$this$FOR$EACH$score$in$the$list$
$$$$$$$$$$$$$$(cond$[(or$(<$x$0)
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$(>$x$100))$$--->$if$our$score$is$an$impossible$score,$we$are$going$to$return$an$error$message
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$(error$x$"is$not$a$valid$grade.")]
$$$$$$$$$$$$$$$$$$$$$$$$$[else$(set!$Sum$(+$sum$x))]$-->$if$it$is$NOT$an$invalid$score,$add$the$score$to$sum$
Lst))]
$$$(help)))$----->$we$call$our$help$procedure$to$call$FOR$EACH$on$every$item$in$this$list,$and$add$it$to$the$sumBUT$we$can't$just$call$ help$
(begin$(help)
$$$$$$$$$$$$$(if$(>$num-grades$0)
$$$$$$$$$$$$$$$$$$(\ sum$num-grades)
$$$$$$$$$$$$$$$$$$(error$"there$were$no$grades$in$the$list."))))
If$you$want$to$check$these$error$messages:
(check-error$(compute-grade$empty)$$"there$were$no$grades$in$the$list.")
This$is$the$animal-weight$mutator,$which$allows$us$to$CHANGE$the$animal$weight$
Imperative*Programming:*Mutation*(cont.)
Friday,$March$2,$2018 12:06$PM
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 9 pages and 3 million more documents.

Already have an account? Log in

Get OneClass Notes+

Unlimited access to class notes and textbook notes.

YearlyBest Value
75% OFF
$8 USD/m
Monthly
$30 USD/m
You will be charged $96 USD upfront and auto renewed at the end of each cycle. You may cancel anytime under Payment Settings. For more information, see our Terms and Privacy.
Payments are encrypted using 256-bit SSL. Powered by Stripe.