Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] [ANN] The Missing Library
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-04-28 (13:29)
From: Andreas Rossberg <rossberg@p...>
Subject: Re: [Caml-list] [ANN] The Missing Library
skaller wrote:
>>Why can't you do this kind of in ocaml?  Returning something like a
>>"next" function would seem to give you a very basic (but usable)
>>iterator.  Which part of the iterator abstraction can't you do?
> I suggest you try it. I don't know how to answer the question.
> I think the answer is 'C++ templates provide functorial
> polymorphism (polyadicity), Ocaml has no such expressive power.'

It has, but only on the module level.

Anyway, I don't think it is needed for generic iteration. The most 
important aspects of iterators easily can be captures by either HOFs or 
by lazy streams. For example, a forward iterator just might be an 
arbitrary function of type

   unit -> 'a option

Repeated calls advance the iterator, at the end it returns None.

More functionally, with streams, an iterator is a value of type 'a 
stream, probably definable in OCaml as follows:

   type 'a stream = 'a stream' lazy_t
   and 'a stream' = Nil | Cons of 'a * 'a stream

Essentially, you iterate over (parts of) arbitrary structure by turning 
them into lazy lists. Using laziness to move from a push to a pull model 
(which is what your control inversion means, IIUC) is rather a standard 
FP technique.

The main difference wrt C++ style iterators is that you need to give the 
end of the iteration in advance (if you want some particular range). I 
don't think you lose something important by that limitation.

	 - Andreas

Andreas Rossberg,

Let's get rid of those possible thingies!  -- TB

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: