[racket] why is the comparimng always false
-----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-----