[racket] ufo structure problem

From: Roelof Wobben (r.wobben at home.nl)
Date: Mon Apr 2 07:29:31 EDT 2012

Op 2-4-2012 13:02, Stephen Bloch schreef:
> On Apr 2, 2012, at 3:25 AM, Pierpaolo Bernardi quoted:
>
>>> I thought that the position of the ufo can be find at (posn-y p) and
>>> (posn-x) because the postition is a struct of Posn.
> and replied:
>
>> You must pay more attention to small details. What is the p in (posn-y
>> p)?  Isn't an argument missing in (posn-x)?
>> What is the u in (ufo-loc u)  (that's the variable that DrRacket says
>> it doesn't know).
> When I teach structs, I tell my students to write down the data definition (in a natural-language comment), then a define-struct, then contracts for all the "functions that come for free."  For example, if I were defining "posn" from scratch, it would look like
>
> ; A posn has two numbers (x and y).
> (define-struct posn [x y])
> ; make-posn : number(x) number(y) ->  posn
> ; posn-x : posn ->  number
> ; posn-y : posn ->  number
> ; posn? : anything ->  boolean
>
> That's in the HtDP student languages, of course; in #lang racket the constructor would be named posn rather than make-posn.
>
> Once you've written these contracts, make sure you're actually following them.  "(posn-x)" doesn't follow its contract because posn-x is supposed to take a parameter.
> "p-x" doesn't follow the contract because it's not defined at all.
> "(posn-y u)" doesn't follow the contract because u is a ufo, and posn-y is supposed to take in a posn.
>
> And so on.
>
>
>
> Stephen Bloch
> sbloch at adelphi.edu
>
>

Like you say U is the variable ufo.
I like the way you do these things.
but what did you mean with "p-x" ?

Following your way of working the answer is (posn-x p) and (posn-y p) 
because P is the struct which holds the position.

Roelof


Posted on the users mailing list.