unfold

Matthew Fluet mfluet@intertrust.com
Mon, 30 Jul 2001 17:49:43 -0700 (PDT)


> It's mostly a question of library consistency.  Since for lists, the old unfold
> can be efficiently implemented in terms of the new unfold but not vice-versa, I
> believe the new unfold should be in the LIST signature.  But I like functions
> with the same name to have the same type (e.g. List.fold, Vector.fold, ...).
> So, the question is, should the new unfold be called "unfold" or should the old
> one (I would say old).  Assuming we keep the old one as unfold, what is the
> right name for the new one.  The best I can think of is unfold'.

I'd vote for the following:

      val fold: 'a t * 'b * ('a * 'b -> 'b) -> 'b
      val foldi: 'a t * 'b * (int * 'a * 'b -> 'b) -> 'b

      val unfold: 'b * ('b -> ('a * 'b) option) -> 'a t
      val unfoldi: int * 'b * (int * 'b -> ('a * 'b)) -> 'a t

(Note: this changes the type of the old unfold.  Why didn't the unfolding
function get the index?  Yes, you could always carry it in the state.)

> 8 different unfolds: unfold, unfold', unfoldi, unfoldi', unfoldr,
> unfoldr', unfoldri, unfoldri'.

How could you have unfoldi and unfoldi'?  Particularly for vector/arrays?
What am I supposed to do if my generator returns NONE before I've filled
up the vector/array?