[racket] why is the comparimng always false

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Sun Jul 1 13:12:26 EDT 2012


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. 

explanation: The definition of structures introduces one more thing that is not explained. For example, 

(define-struct Vcat (x y))

introduces 
make-Vcat 
Vcat-x 
Vcat-y
Vcat? 

AND 

Vcat. 

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. 

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.) 

Hope this helps, and sorry for the confusion -- Matthias




On Jun 30, 2012, at 3:20 PM, Roelof Wobben wrote:

> Op 30-6-2012 15:55, Matthias Felleisen schreef:
>> 
>> On Jun 30, 2012, at 3:03 AM, Roelof Wobben wrote:
>> 
>>> I can do the same for my problem.
>>> 
>>> ; A Vanimal is one of :
>>> ; - (make-Vcat ( x happiness) 
>>> ;   (make-Vcham ( x happiness) 
>>> 
>>> So Vanimal is not a struct but strictly a name.
>>> 
>>> Am I on the right track. 
>> 
>> 
>> YES! 
>> 
> 
> Oke, 
> 
> Last problem I hope.
> 
> I have now this : 
> 
> 
> ; niet grafische constanten.
> 
> (define lengte-werkblad 200)
> (define breedte-werkblad 1000)
> (define move-animal 3)
> (define move-gauge 0.1)
> 
> ; grafische constanten
> 
> (define kat  .)
> (define cham .)
> (define workspace (empty-scene breedte-werkblad lengte-werkblad))
> (define gauge-omtrek (rectangle 1000 20 "outline" "black"))
> 
> 
> ; berekende constanten
> (define correctie-cat ( / (image-width kat)2))
> (define ondergrens-cat ( - 0  correctie-cat))
> (define bovengrens-cat ( + breedte-werkblad correctie-cat))
> 
> (define correctie-cham ( / (image-width cham)2))
> (define ondergrens-cham ( - 0  correctie-cham))
> (define bovengrens-cham ( + breedte-werkblad correctie-cham))
> 
> ; Te gebruiken structs
> 
> ; Design a world program that works with both cats and chameleons: 
> ; A VAnimal is either
> ; – a VCat
> ; – a VCham
> 
> (define-struct Vcat (Xcat Hcat Richting))
> ; Vcat = (make-editor Number Number)
> ; interp. (make-editor x h) where x is the x-coordinate of the cat and h is the happiness of the cat.
> ; make-editor Number Number -> Vcat
> ; Vcat-Xcat Editor -> Number
> ; Vcat-Hcat Editor -> Number
> ; Vcat-Richting -> String
> ; Vcat? Editor Any -> Boolean
> 
> 
> (define-struct Vcham (Xcham Hcham Richting))
> ; Vcham = (make-editor Number Number)
> ; interp. (make-editor x h) where x is the x-coordinate of the cham and h is the happiness of the cham
> ; make-editor Number Number -> Vcat
> ; Vcham-Xcham Editor -> Number
> ; Vcham-Hcham Editor -> Number
> ; Vcham-Richting -> String
> ; Vcham? Editor Any -> Boolean
> 
> 
> ; Vcham -> Vcham
> ; Function who makes the image move to the left or turn to the right
> (check-expect (links-of-draaien-cham (make-Vcham 20 100 "left")) (make-Vcham 17 99.9 "left"))
> (check-expect (links-of-draaien-cham (make-Vcham -39 100 "left")) (make-Vcham -39 99.9 "right"))
> (check-expect (links-of-draaien-cham (make-Vcham 1037 100 "left")) (make-Vcham 1034 99.9 "left"))
> (define (links-of-draaien-cham s)
> (if ( < (Vcham-Xcham Vcham) ondergrens-cham)
>    (make-Vcham  (Vcham-Xcham s)  (- (Vcham-Hcham s) move-gauge) "right")
>    (make-Vcham (- (Vcham-Xcham s) move-animal) (- (Vcham-Hcham s) move-gauge) "left")))
> 
> 
> 
> ; Vcat -> Vcat
> ; Function who makes the image move to the left or turn to the right
> (check-expect (links-of-draaien-cat (make-Vcat 20 100 "left")) (make-Vcat 17 99.9 "left"))
> (check-expect (links-of-draaien-cat (make-Vcat -39 100 "left")) (make-Vcat -39 99.9 "right"))
> (check-expect (links-of-draaien-cat (make-Vcat 1037 100 "left")) (make-Vcat 1034 99.9 "left"))
> (define (links-of-draaien-cat s)
> (if ( < (Vcat-Xcat Vcat) ondergrens-cat)
>    (make-Vcat  (Vcat-Xcat s)  (- (Vcat-Hcat s) move-gauge) "right")
>    (make-Vcat (- (Vcat-Xcat s) move-animal) (- (Vcat-Hcat s) move-gauge) "left"))
> )
> 
> ; Vcham -> Vcham
> ; Function who makes the image move to the right or turn to the left
> (check-expect (rechts-of-draaien-cham (make-Vcham 20 100 "right")) (make-Vcham 23 99.9 "right"))
> (check-expect (rechts-of-draaien-cham (make-Vcham -39 100 "right")) (make-Vcham -36 99.9 "right"))
> (check-expect (rechts-of-draaien-cham (make-Vcham 1040 100 "right")) (make-Vcham 1040 99.9 "left"))
> (define (rechts-of-draaien-cham s)
> (if ( > (Vcham-Xcham Vcham) bovengrens-cham)
>    (make-Vcham  (Vcham-Xcham s)  (- (Vcham-Hcham s) move-gauge) "left")
>    (make-Vcham (+ (Vcham-Xcham s) move-animal) (- (Vcham-Hcham s) move-gauge) "right")))
>   
>   
> ; Vcat -> Vcat
> ; Function who makes the image move to the right or turn to the left
> (check-expect (rechts-of-draaien-cat (make-Vcat 20 100 "right")) (make-Vcat 23 99.9 "right"))
> (check-expect (rechts-of-draaien-cat (make-Vcat -39 100 "right")) (make-Vcat -36 99.9 "right"))
> (check-expect (rechts-of-draaien-cat (make-Vcat 1040 100 "right")) (make-Vcat 1040 99.9 "left"))
> (define (rechts-of-draaien-cat s)
> (if ( > (Vcat-Xcat Vcat) bovengrens-cat)
>    (make-Vcat  (Vcat-Xcat s)  (- (Vcat-Hcat s) move-gauge) "left")
>    (make-Vcat (+ (Vcat-Xcat s) move-animal) (- (Vcat-Hcat s) move-gauge) "right"))
> )
> 
> ; Vanimal -> Vanimal
> ; Function which change the world on clock ticks.
> ; On every tick the X-coordinate changes 3 pixels and the happiness decrease with 0.1.
>   (define (tock s)
>     (cond 
>       [(Vcat? s)(if(equal? (Vcat-Richting s) "left") (links-of-draaien-cat Vcat) (rechts-of-draaien-cat Vcat))]
>       [(Vcham? s) (if(equal? (Vcham-Richting s) "left") (links-of-draaien-cham Vcham) (rechts-of-draaien-cat Vcham))]
> ))
> 
> ; Vanimal -> Image
> ; Function who display the state of the world into a image.
> (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)))
> (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)))
> (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)))
> (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)))
> (define (render s)
>   [cond 
>     [(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))]
>     [ (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))]
> ])
> 
> ; VCat keyevent -> Vcat
> ; Function who changes the world on a key-event
> (check-expect (happy-cat (make-Vcat 90 10 "right")"up") (make-Vcat 90 12 "right"))
> (check-expect (happy-cat (make-Vcat 90 100 "left")"down") (make-Vcat 90 100 "left"))
> (define (happy-cat Vcat k)
>   (cond
>     [ (and (key=? k "up") (< (Vcat-Hcat Vcat) 100))   (make-Vcat (Vcat-Xcat Vcat) (+ (Vcat-Hcat Vcat)2) (Vcat-Richting Vcat))]
>     [ (and (key=? k "down") (< (Vcat-Hcat Vcat) 100)) (make-Vcat (Vcat-Xcat Vcat) (+ (Vcat-Hcat Vcat)5) (Vcat-Richting Vcat))]
>     [else Vcat]
>     ))
> 
> ; VCham keyevent -> Vcat
> ; Function who changes the world on a key-event
> (check-expect (happy-cham (make-Vcham 90 10 "right")"up") (make-Vcham 90 12 "right"))
> (check-expect (happy-cham (make-Vcham 90 100 "left")"down") (make-Vcham 90 100 "left"))
> (define (happy-cham Vcham k)
>   (cond
>     [ (and (key=? k "up") (< (Vcat-Hcat Vcat) 100))   (make-Vcat (Vcat-Xcat Vcat) (+ (Vcat-Hcat Vcat)2) (Vcat-Richting Vcat))]
>     [else Vcat]
>     ))
> 
> (define (happy s k)
>   (cond 
>     [ (Vcat? s) (happy-cat s k)]
>     [ (Vcham? s) (happy-cham s k)]
>     ))
> 
> (define (stop s)
>   (cond 
>     [ (Vcat? s) (equal? (Vcat-Hcat s) 0)]
>     [  (Vcham? s) (equal? (Vcham-Hcham s) 0)]
>     ))
> 
> (define (main s)
>   (cond 
>     [(Vcat? s) (big-bang s (check-with Vcat?) (on-tick tock) (on-draw render) )]
>     [(Vcham? s) (big-bang s (check-with Vcham?) (on-tick tock) (on-draw render) )]
>     ))
> 
> (main (make-Vcat 12 100 "left"))
> 
> But it fails with this : 
> 
> Vcat-Xcat: expects a Vcat; given (make-signature ...)
> Vcham-Xcham: expects a Vcham; given (make-signature ...)
> 
> Can it be a problem that on both I call the structure s ?
> 
> Roelof
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20120701/9c54005c/attachment-0001.html>

Posted on the users mailing list.