Rewrite Hamarabi in lisp.
*Start with year = 0, Pop =100, Food = 100, and Garden = 0
*Ask
them how much to dedicate to growing food, making babies, or building
the garden.
Then
Year=Year+1
*Garden = Garden +
PeopleWorkingOnGarden
*Pop = integer(pop * 0.95 +
PeopleMaingBabies/2)
*Food = 2 * PeopleMakingFood + 0.5 *
Food
*if (Food > Pop)
Food = Food –
Pop
Else
Pop = Food
Food = 0
*No input should be negative
*The inputs should not exceed
availale resources
*They win if Garden > 100
Hints:
(random 17) returns a number 0 ... 16
(read)
returns the number they just typed in
(display "you have
killed all your goats") sends a message to the user
This is worth 9 points (one per '*').
Make the following functions
Get: Given a property name
and an alist, return the associated value.
(get 'age '((height
19) (age 3) (weight 12))) returns '3.
(get 'grade '((height 19)
(age 3) (weight 12))) returns '().
Set: Given a property name,
a property value, and an alist, returns a new alist with the
property-value tuple in the returned list exactly once. Order of
elements in the alist does not matter.
(set 'age 12 '((height 19)
(age 3) (weight 12))) returns '((height 19) (age 12) (weight 12))
Delete: Given a property name, and an alist, returns a
new alist with the property-value removed. Order of elements in the
alist does not matter.
(delete 'age '((height 19) (age 3) (weight
12))) returns '((height 19) (weight 12))
I'm fully aware that the web is filled with sample code to do these things. Use of such code is cheating! I shall check.
(define (test) (display (set 'a 1 '((a 10) (b 11) (c 12)))) (newline) (display (set 'd 4 '((a 10) (b 11) (c 12)))) (newline) (display (eqv? 10 (get 'age (set 'age 10 '(()))))) (display (eqv? '() (get 'age '(())))) (display (eqv? 10 (get 'age (set 'age 10 '((age 8) (height 6) (weight 3)))))) (display (eqv? 10 (get 'height (set 'height 10 '((age 8) (height 6) (weight 3)))))) (display (eqv? 10 (get 'age (set 'age 10 '((width 4) (age 8) (height 6) (weight 3)))))) (display (eqv? 7 (get 'weight (set 'weight 7 '((age 8) (height 6) (weight 3)))))) (display (eqv? 'fifth (get 'grade (set 'grade 'fifth '((age 8) (height 6) (weight 3)))))) (display (eqv? '() (get 'missing (set 'age 10 '((age 8) (height 6) (weight 3)))))) (display (eqv? 10 (get 'age (set 'age 10 '())))) (display '(“Deleting spam from list”)) (display (delete 'spam '((spam 10)))) (display '(“Deleting spam from list”)) (display (delete 'spam '((nospam 10)))) (display '(“Deleting spam from list”)) (display (delete 'spam '())) (display '(“Deleting spam from list”)) (display (delete 'spam '((fred 5) (steve 7) (spam 10)))) (display '(“Deleting spam from list”)) (display (delete 'spam '((spam 10) (wilma 4) (fred 2) (fry yes)))) (display '(“Deleting spam from list”)) (display (delete 'spam '((wilma 10) (spam yes) (fred 2) (fry yes)))) (display '(“Deleting spam from list”)) (display (delete 'spam '((nospam 10) (wilma 4) (fred 2) (fry yes)))) )