You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This problem is visually complex. I can't understand it at a glance without getting back into the original HTDP lesson and problem. It's probably simple if looked at in a different way?
Sketching in different ways (like a tree diagram) page 169
; A VAnimal is either; – a VCat; – a VCham
Struggling a bit on best practice with conditional ... understand the goal is to reduce repetition, but each route seems to have it's pros/cons:
My attempt (below)
For each (cond ...) pass to a separate function
Each condition has it's own conditionals, per Type (VCat/VCham)
Create a (cond ..) expression once for each enumeration
Create an object instance (make-struct ...)once and only once for VCat/VCham
Both methods pass only raw data values to auxiliary functions.
; VAnimal KeyEvent -> VAnimal; pet, or feed a VCat; feed a VCham, change color
(define (commands a ke)
(cond
[(vcat? a) (vcat-commands a ke)]
[(vcham? a) (vcham-commands a ke)]))
; VCat KeyEvent -> VCat; Check the key events
(define (vcat-commands c ke)
(cond
[(key=? ke "up")
(make-vcat (vcat-pos c)
(make-happy (vcat-happy c)))] ; pet
[(key=? ke "down")
(make-vcat (vcat-pos c)
(make-happy (vcat-happy c)))] ; feed
[else c])) ; #!; VCham KeyEvent -> VCham; Check the key events
(define (vcham-commands c ke)
(cond
[(key=? ke "down") ; feed
(make-vcham (vcham-pos c)
(make-happy (vcham-happy c))
(vcham-color c))]
[(key=? ke "r") (change-color c RED)]
[(key=? ke "b") (change-color c BLUE)]
[(key=? ke "g") (change-color c GREEN)]
[else c])) ; #!
A sketch of each "tree":
More detail:
The text was updated successfully, but these errors were encountered:
This problem is visually complex. I can't understand it at a glance without getting back into the original HTDP lesson and problem. It's probably simple if looked at in a different way?
Struggling a bit on best practice with conditional ... understand the goal is to reduce repetition, but each route seems to have it's pros/cons:
(cond ...)
pass to a separate functionType
(VCat
/VCham
)(cond ..)
expression once for each enumeration(make-struct ...)
once and only once forVCat
/VCham
Both methods pass only raw data values to auxiliary functions.
A sketch of each "tree":
More detail:
The text was updated successfully, but these errors were encountered: