<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><br></div><div><br></div><div>Please choose variable names other than Vcat and such. I like a-vcat for example. This name emphasiszes that I get one particular vcat here -- not all of the infinitely many possible ones. </div><div><br></div><div>explanation: The definition of structures introduces one more thing that is not explained. For example, </div><div><br></div><div>(define-struct Vcat (x y))</div><div><br></div><div>introduces </div><div>make-Vcat </div><div>Vcat-x </div><div>Vcat-y</div><div>Vcat? </div><div><br></div><div>AND </div><div><br></div><div>Vcat. </div><div><br></div><div>The latter is something like a "type" that can be used to express signatures as a part of the programming language and get DrRacket to check them as your program runs. As soon as it fails, it will let you know. </div><div><br></div><div>Checked signatures are available only in ISL and up, and their documentation is in German. (No I didn't write them, and I haven't translated them yet.) </div><div><br></div><div>Hope this helps, and sorry for the confusion -- Matthias</div><div><br></div><div><br></div><div><br></div><br><div><div>On Jun 30, 2012, at 3:20 PM, Roelof Wobben wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">
<meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type">
<div bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix">Op 30-6-2012 15:55, Matthias Felleisen
schreef:<br>
</div>
<blockquote cite="mid:0CB0AB61-3C99-4E43-ADD5-EB70C4C1EADC@ccs.neu.edu" type="cite"><br>
<div>
<div>On Jun 30, 2012, at 3:03 AM, Roelof Wobben wrote:</div>
<br class="Apple-interchange-newline">
<blockquote type="cite"><span class="Apple-style-span" style="border-collapse: separate; font-family: 'Lucida
Grande'; font-style: normal; font-variant: normal;
font-weight: normal; letter-spacing: normal; line-height:
normal; orphans: 2; text-align: -webkit-auto; text-indent:
0px; text-transform: none; white-space: normal; widows: 2;
word-spacing: 0px; -webkit-border-horizontal-spacing: 0px;
-webkit-border-vertical-spacing: 0px;
-webkit-text-decorations-in-effect: none;
-webkit-text-size-adjust: auto; -webkit-text-stroke-width:
0px; font-size: medium; ">I can do the same for my problem.<br>
<br>
; A Vanimal is one of :<br>
; - (make-Vcat ( x happiness)<span class="Apple-converted-space"> </span><br>
; (make-Vcham ( x happiness)<span class="Apple-converted-space"> </span><br>
<br>
So Vanimal is not a struct but strictly a name.<br>
<br>
Am I on the right track.<span class="Apple-converted-space"> </span></span></blockquote>
</div>
<br>
<div><br>
</div>
<div>YES! </div>
<div><br>
</div>
</blockquote>
<br>
Oke, <br>
<br>
Last problem I hope.<br>
<br>
I have now this : <br>
<br>
<br>
; niet grafische constanten.<br>
<br>
(define lengte-werkblad 200)<br>
(define breedte-werkblad 1000)<br>
(define move-animal 3)<br>
(define move-gauge 0.1)<br>
<br>
; grafische constanten<br>
<br>
(define kat .)<br>
(define cham .)<br>
(define workspace (empty-scene breedte-werkblad lengte-werkblad))<br>
(define gauge-omtrek (rectangle 1000 20 "outline" "black"))<br>
<br>
<br>
; berekende constanten<br>
(define correctie-cat ( / (image-width kat)2))<br>
(define ondergrens-cat ( - 0 correctie-cat))<br>
(define bovengrens-cat ( + breedte-werkblad correctie-cat))<br>
<br>
(define correctie-cham ( / (image-width cham)2))<br>
(define ondergrens-cham ( - 0 correctie-cham))<br>
(define bovengrens-cham ( + breedte-werkblad correctie-cham))<br>
<br>
; Te gebruiken structs<br>
<br>
; Design a world program that works with both cats and chameleons: <br>
; A VAnimal is either<br>
; – a VCat<br>
; – a VCham<br>
<br>
(define-struct Vcat (Xcat Hcat Richting))<br>
; Vcat = (make-editor Number Number)<br>
; interp. (make-editor x h) where x is the x-coordinate of the cat
and h is the happiness of the cat.<br>
; make-editor Number Number -> Vcat<br>
; Vcat-Xcat Editor -> Number<br>
; Vcat-Hcat Editor -> Number<br>
; Vcat-Richting -> String<br>
; Vcat? Editor Any -> Boolean<br>
<br>
<br>
(define-struct Vcham (Xcham Hcham Richting))<br>
; Vcham = (make-editor Number Number)<br>
; interp. (make-editor x h) where x is the x-coordinate of the cham
and h is the happiness of the cham<br>
; make-editor Number Number -> Vcat<br>
; Vcham-Xcham Editor -> Number<br>
; Vcham-Hcham Editor -> Number<br>
; Vcham-Richting -> String<br>
; Vcham? Editor Any -> Boolean<br>
<br>
<br>
; Vcham -> Vcham<br>
; Function who makes the image move to the left or turn to the right<br>
(check-expect (links-of-draaien-cham (make-Vcham 20 100 "left"))
(make-Vcham 17 99.9 "left"))<br>
(check-expect (links-of-draaien-cham (make-Vcham -39 100 "left"))
(make-Vcham -39 99.9 "right"))<br>
(check-expect (links-of-draaien-cham (make-Vcham 1037 100 "left"))
(make-Vcham 1034 99.9 "left"))<br>
(define (links-of-draaien-cham s)<br>
(if ( < (Vcham-Xcham Vcham) ondergrens-cham)<br>
(make-Vcham (Vcham-Xcham s) (- (Vcham-Hcham s) move-gauge)
"right")<br>
(make-Vcham (- (Vcham-Xcham s) move-animal) (- (Vcham-Hcham s)
move-gauge) "left")))<br>
<br>
<br>
<br>
; Vcat -> Vcat<br>
; Function who makes the image move to the left or turn to the right<br>
(check-expect (links-of-draaien-cat (make-Vcat 20 100 "left"))
(make-Vcat 17 99.9 "left"))<br>
(check-expect (links-of-draaien-cat (make-Vcat -39 100 "left"))
(make-Vcat -39 99.9 "right"))<br>
(check-expect (links-of-draaien-cat (make-Vcat 1037 100 "left"))
(make-Vcat 1034 99.9 "left"))<br>
(define (links-of-draaien-cat s)<br>
(if ( < (Vcat-Xcat Vcat) ondergrens-cat)<br>
(make-Vcat (Vcat-Xcat s) (- (Vcat-Hcat s) move-gauge) "right")<br>
(make-Vcat (- (Vcat-Xcat s) move-animal) (- (Vcat-Hcat s)
move-gauge) "left"))<br>
)<br>
<br>
; Vcham -> Vcham<br>
; Function who makes the image move to the right or turn to the left<br>
(check-expect (rechts-of-draaien-cham (make-Vcham 20 100 "right"))
(make-Vcham 23 99.9 "right"))<br>
(check-expect (rechts-of-draaien-cham (make-Vcham -39 100 "right"))
(make-Vcham -36 99.9 "right"))<br>
(check-expect (rechts-of-draaien-cham (make-Vcham 1040 100 "right"))
(make-Vcham 1040 99.9 "left"))<br>
(define (rechts-of-draaien-cham s)<br>
(if ( > (Vcham-Xcham Vcham) bovengrens-cham)<br>
(make-Vcham (Vcham-Xcham s) (- (Vcham-Hcham s) move-gauge)
"left")<br>
(make-Vcham (+ (Vcham-Xcham s) move-animal) (- (Vcham-Hcham s)
move-gauge) "right")))<br>
<br>
<br>
; Vcat -> Vcat<br>
; Function who makes the image move to the right or turn to the left<br>
(check-expect (rechts-of-draaien-cat (make-Vcat 20 100 "right"))
(make-Vcat 23 99.9 "right"))<br>
(check-expect (rechts-of-draaien-cat (make-Vcat -39 100 "right"))
(make-Vcat -36 99.9 "right"))<br>
(check-expect (rechts-of-draaien-cat (make-Vcat 1040 100 "right"))
(make-Vcat 1040 99.9 "left"))<br>
(define (rechts-of-draaien-cat s)<br>
(if ( > (Vcat-Xcat Vcat) bovengrens-cat)<br>
(make-Vcat (Vcat-Xcat s) (- (Vcat-Hcat s) move-gauge) "left")<br>
(make-Vcat (+ (Vcat-Xcat s) move-animal) (- (Vcat-Hcat s)
move-gauge) "right"))<br>
)<br>
<br>
; Vanimal -> Vanimal<br>
; Function which change the world on clock ticks.<br>
; On every tick the X-coordinate changes 3 pixels and the happiness
decrease with 0.1.<br>
(define (tock s)<br>
(cond <br>
[(Vcat? s)(if(equal? (Vcat-Richting s) "left")
(links-of-draaien-cat Vcat) (rechts-of-draaien-cat Vcat))]<br>
[(Vcham? s) (if(equal? (Vcham-Richting s) "left")
(links-of-draaien-cham Vcham) (rechts-of-draaien-cat Vcham))]<br>
))<br>
<br>
; Vanimal -> Image<br>
; Function who display the state of the world into a image.<br>
(check-expect (render (make-Vcat 90 100 "left")) (place-image
(overlay/xy gauge-omtrek 0 0 (rectangle 1000 20 "solid" "red")) 500
10 (place-image kat 90 140 workspace)))<br>
(check-expect (render (make-Vcat 0 100 "right")) (place-image
(overlay/xy gauge-omtrek 0 0 (rectangle 1000 20 "solid" "red")) 500
10 (place-image kat 0 140 workspace)))<br>
(check-expect (render (make-Vcham 90 100 "left")) (place-image
(overlay/xy gauge-omtrek 0 0 (rectangle 1000 20 "solid" "red")) 500
10 (place-image cham 90 140 workspace)))<br>
(check-expect (render (make-Vcham 0 100 "right")) (place-image
(overlay/xy gauge-omtrek 0 0 (rectangle 1000 20 "solid" "red")) 500
10 (place-image cham 0 140 workspace)))<br>
(define (render s)<br>
[cond <br>
[(Vcat? s) (place-image (overlay/xy gauge-omtrek 0 0
(rectangle (* 10 (Vcat-Hcat s)) 20 "solid" "red")) 500 10
(place-image kat (Vcat-Xcat s) 140 workspace))]<br>
[ (Vcham? s) (place-image (overlay/xy gauge-omtrek 0 0
(rectangle (* 10 (Vcham-Hcham s)) 20 "solid" "red")) 500 10
(place-image cham (Vcham-Xcham s) 140 workspace))]<br>
])<br>
<br>
; VCat keyevent -> Vcat<br>
; Function who changes the world on a key-event<br>
(check-expect (happy-cat (make-Vcat 90 10 "right")"up") (make-Vcat
90 12 "right"))<br>
(check-expect (happy-cat (make-Vcat 90 100 "left")"down") (make-Vcat
90 100 "left"))<br>
(define (happy-cat Vcat k)<br>
(cond<br>
[ (and (key=? k "up") (< (Vcat-Hcat Vcat) 100)) (make-Vcat
(Vcat-Xcat Vcat) (+ (Vcat-Hcat Vcat)2) (Vcat-Richting Vcat))]<br>
[ (and (key=? k "down") (< (Vcat-Hcat Vcat) 100)) (make-Vcat
(Vcat-Xcat Vcat) (+ (Vcat-Hcat Vcat)5) (Vcat-Richting Vcat))]<br>
[else Vcat]<br>
))<br>
<br>
; VCham keyevent -> Vcat<br>
; Function who changes the world on a key-event<br>
(check-expect (happy-cham (make-Vcham 90 10 "right")"up")
(make-Vcham 90 12 "right"))<br>
(check-expect (happy-cham (make-Vcham 90 100 "left")"down")
(make-Vcham 90 100 "left"))<br>
(define (happy-cham Vcham k)<br>
(cond<br>
[ (and (key=? k "up") (< (Vcat-Hcat Vcat) 100)) (make-Vcat
(Vcat-Xcat Vcat) (+ (Vcat-Hcat Vcat)2) (Vcat-Richting Vcat))]<br>
[else Vcat]<br>
))<br>
<br>
(define (happy s k)<br>
(cond <br>
[ (Vcat? s) (happy-cat s k)]<br>
[ (Vcham? s) (happy-cham s k)]<br>
))<br>
<br>
(define (stop s)<br>
(cond <br>
[ (Vcat? s) (equal? (Vcat-Hcat s) 0)]<br>
[ (Vcham? s) (equal? (Vcham-Hcham s) 0)]<br>
))<br>
<br>
(define (main s)<br>
(cond <br>
[(Vcat? s) (big-bang s (check-with Vcat?) (on-tick tock)
(on-draw render) )]<br>
[(Vcham? s) (big-bang s (check-with Vcham?) (on-tick tock)
(on-draw render) )]<br>
))<br>
<br>
(main (make-Vcat 12 100 "left"))<br>
<br>
But it fails with this : <br>
<br>
Vcat-Xcat: expects a Vcat; given (make-signature ...)<br>
Vcham-Xcham: expects a Vcham; given (make-signature ...)<br>
<br>
Can it be a problem that on both I call the structure s ?<br>
<br>
Roelof<br>
<br>
</div>
</blockquote></div><br></body></html>