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

From: Carl Eastlund (carl.eastlund at gmail.com)
Date: Sun Dec 11 01:28:20 EST 2005

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?

To do any of this, you would have to redefine define-struct (or define
an alternate define-struct-like form).  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.

--Carl


Posted on the users mailing list.