[plt-scheme] Using student-defined structures in a teachpack

From: Mike Burns (mike at mike-burns.com)
Date: Sun Dec 11 01:57:41 EST 2005

On 2005-12-11 01.28.20 -0500, Carl Eastlund wrote:
> On 12/11/05, Mike Burns <mike at mike-burns.com> wrote:
> > I'd like to have a teachpack which allows the student to define her own
> > Point struct such that the functions in the teachpack work with the
> > student-defined Point. For example,
> >
> > Teachpack:
> >
> >   (module a mzscheme
> >    (provide point->pair)
> >
> >    (define (point->pair a-point)
> >     (cons (point-x a-point) (point-y a-point))))
> >
> > Definitions window (with the above teachpack loaded):
> >
> >   (define-struct point [x y])
> >   (point->pair (make-point 1 2))
> >
> > Any tips on how such a teachpack could be written?
> I'm a little unclear on your purpose here, and on how exactly you
> expect this to work.  As shown, obviously this won't work because
> point-x and point-y are undefined in your teachpack.  Is your
> intention that point->pair will only work for a structure whose
> definition is precisely "(define-struct point (x y))"?  What if the
> student names it Point, or the fields a and b, or has a third field z?
>  Should point->pair work for those structures, not work for those
> structures, or would you expect a compile error?

Shouldn't work on capital-p-Point, or with fields a and b, but a third field 
shouldn't be a problem.

> To do any of this, you would have to redefine define-struct (or define
> an alternate define-struct-like form).

Excellent, I'll look into that.

> From a teaching standpoint,
> this is all a very strange thing to do - requiring the student to
> define the datatype, but providing operations on it.  Why not either
> write the functions for Posn, the provided point-like datatype, or
> require the student to write them as well?  In any event, changing the
> relationship between structure definitions and the functions defined
> on a datatype is more likely to confuse a student than anything else.

I'm actually wrapping functions which take Posns. The idea was to have the 
student create her own data structure but have it play nicely with the rest 
of the system, giving them quick satisfaction. The problem they need to 
solve is something like:

  We've been dealing with single values, but to play Battleship we need two 
  numbers. Once you have a way of representing two numbers then we can play 

You do raise good points; maybe we'll just use Posns instead.

Mike Burns mike at mike-burns.com http://mike-burns.com

Posted on the users mailing list.