[racket] why is the comparimng always false

From: Roelof Wobben (r.wobben at home.nl)
Date: Fri Jun 29 15:14:40 EDT 2012

Thanks for the remark.

I have now this as a solution :


; 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 Vanimal (Vchat 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 Vcat) (rechts-of-draaien-cat Vcham))]
))

(define (render Vanimal)Vanimal)

(define (main Vanimal)
   (big-bang Vanimal (check-with Vanimal?) (on-tick tock) (on-draw 
render) ))

(main (make-Vcat 12 100 "left"))

But it fails with this message : check-with: the initial expression 
evaluated to (make-Vcat 12 100 "left"), which fails to pass check-with's 
Vanimal? test

So I think I still not understand the relation between Vcat and Vcham on 
one side and Vanimal on the other side.

Roelof





12 9:06, Marijn schreef:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On 27-06-12 15:56, Matthias Felleisen wrote:
>>
>> 1. You got trapped in a strange corner of the language:
>>
>> -- to ask whether some value is a a Vcat you should use Vcat? --
>> BSL should not really allow you to say 'Vcat' all by itself.
> Actually the program below says `Vchat' which isn't defined in the
> code. Roelof, is the below program really what you tried (it seems to
> also be missing a parenthesis)?
>
> Disregarding that typo the problem seems to be that the program is
> creating a Vanimal out of either a Vcat or a Vcham and then asking
> whether the Vanimal object/instance is `equal?' to Vc(h)at (the struct
> descriptor?), which then naturally results in a negative result. Even
> if the program first got a hold of the animal member of Vanimal you
> cannot expect that Vcat or Vcham object to be `equal?' to something
> that you don't know what it is.
>
>> [I need to investigate this problem. Sadly it is not a bug.]
>>
>>
>> 2. Try to understand this:
>>
>> (define-struct Vcat [x happiness]) ;; A Vcat is (make-Vcat Number
>> Number)
>>
>> (define-struct Vcham [x happiness]) ;; A Vcham is (make-Vcat Number
>> Number)
>>
>>
>> ;; An Animal is one of: ;; -- (make-Vcat Number Number) ;; --
>> (make-Vcham Number Number)
>>
>>
>> ;; Animal -> Number ;; compute the sum of the x location and the
>> degree of happiness
>>
>> (check-expect (happiness-quotient (make-Vcham 10 20)) 30)
>> (check-expect (happiness-quotient (make-Vcat 10 100)) 110)
>>
>> (define (happiness-quotient a) (cond [(Vcat? a) (+ (Vcat-x a)
>> (Vcat-happiness a))] [(Vcham? a) (+ (Vcham-x a) (Vcham-happiness
>> a))]))
>>
>>
>>
>> On Jun 27, 2012, at 5:08 AM, Roelof Wobben wrote:
>>
>>> Hello,
>>>
>>> I try to figure out how I can check if a struct is a Vcat or a
>>> Vcham. So i tried this:
>>>
>>> ; – a VCham (define-struct Vanimal (animal))
>>>
>>>
>>> ; a struct has two parts ( the x-coordinate of a cat and the
>>> happiness of the cat) (define-struct Vcat [x happiness]) ; make
>>> Vcat : number (x) number (h) ; Vcat x -> number ; Vcat h ->
>>> number ; Any Vcat? -> Boolean
>>>
>>> ; a struct has two parts ( the x-coordinate of a kamneleon and
>>> the happiness of the kameleon) (define-struct Vcham [x
>>> happiness]) ; make Vcham : number (x) number (h) ; Vcham x ->
>>> number ; Vcham -> number
>>>
>>> ; Vanimal -> Boolean ; Function which cehcks if a struct is a
>>> Vcat (check-expect (check (make-Vanimal(make-Vcat 0 100))) true)
>>> (check-expect (check (make-Vanimal(make-Vcham 0 100))) false)
>>> (define (check s) ( equal? s Vchat)
>>>
>>> (check(make-Vanimal (make-Vcat 0 100)))
>>>
>>>
>>> But the answer is false not mather if the struct is a Vchat or a
>>> Vcham. Where am I missing something.
> You're missing that a(!) Vanimal is not a(!) Vcat or a(!) Vcham,
> although the Vanimal animal member might be. Further, (no article
> here!) `Vc(h)at' is not a(!) Vc(h)at (whichever way you typo-correct).
>
> Marijn
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v2.0.19 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAk/tU90ACgkQp/VmCx0OL2yDHgCghJHd2x8KJaGalbeLjDzcYeNT
> /fAAn1nwFo/3ozKIxn88giuSYj+GlqTq
> =QSQE
> -----END PGP SIGNATURE-----
>



Posted on the users mailing list.