[racket] ufo structure problem

From: Stephen Bloch (sbloch at adelphi.edu)
Date: Mon Apr 2 07:02:22 EDT 2012

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



Posted on the users mailing list.