fjh at cs.mu.oz.au
Fri Sep 26 19:33:30 AEST 1997
Tomas By, you wrote:
> "Fergus Henderson" <fjh at cs.mu.oz.au> wrote:
> > Tomas By, you wrote:
> > > In general, if I have a procedure with several modes, eg:
> > > [...]
> > > but I can't use the same code for both modes, how do I write
> > > the "top-level"?
> > The way to do this is to pass a argument specifying the direction:
> > [...]
> Hmm. Is there any reason to do it that way rather than have two
> different procedures?:
> :- pred stream_to_tree(stream(T),tree(T)).
> :- mode stream_to_tree(in,out).
> :- pred tree_to_stream(tree(T),stream(T)).
> :- mode tree_to_stream(in,out).
Yes. Often much of the code will be similar, and only a small part
of the code will depend on the direction. By using a single procedure
with multiple modes, you get code reuse, because you can share the
implementation of the common parts, and only write separate code
for the parts that really do depend on the direction.
An exampe of this is when you want a parser and an "unparser"
(i.e. a pretty printer) for a data structure. In that case,
the code may be exactly the same for both directions except
for the handling of whitespace and the low-level I/O.
If the code for the two different directions is entirely different,
then no, I suppose there isn't much reason to use a single predicate.
Fergus Henderson <fjh at cs.mu.oz.au> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger fjh at 184.108.40.206 | -- the last words of T. S. Garp.
More information about the users