[MLton] A Vector/Fold challenge

Matthew Fluet fluet@cs.cornell.edu
Fri, 5 May 2006 11:47:03 -0400 (EDT)


This has been itching me for a few weeks, and while I'm certain I would 
learn more by doing it myself, it will probably come pretty easy to the 
master Fold-ers.

Essentially, I'd like a generalization of Vector.map so that I can map N 
vectors to M vectors via an N-ary product to M-ary product function. 
(Probably, it' makes more sense to start with a generalization of 
Vector.fold, but I seem to recall wanting MtoN maps more often than M 
folds in the compiler.)

That is, I'd love something about this convenient:

  val v1 = Vector.tabulate (10, fn i => i)
  val v2 = Vector.tabulate (10, fn i => 2 * i)

  val (va & vb & vc) =
    Vector.mapNtoM Num.two Num.three
    (fn (x & y) => ((x + y) & (x * y) & (chr x))
    (v1 & v2)

Actually, I guess starting with List.mapNtoM makes more sense, since 
efficiently constructing the resulting vectors seems a little tricky, but 
maybe you can do something clever with Vector.create.

It's not completely clear to me that this can be accomplished with a 
single Num.t type; it seems that these two arguments to Vector.mapNtoM are 
accum-like types, but the former is determining how to rip the product 
apart and the latter is determining how to put the product together, so 
there may not be a unified type there.