Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Caml-list] Streams

2 views
Skip to first unread message

Error404

unread,
Aug 10, 2006, 6:57:59 AM8/10/06
to caml...@inria.fr
Hi,

I'm looking for some streams related tutorial or any other info.
By stream i mean something like this (I don't know exact definition):

open Lazy;;
type 'a stream = Nil | Cons of 'a Lazy.t * 'a stream Lazy.t;;

(* For example stream of 'integers from x' would look like this: *)

let rec intsfrom x =
Cons(lazy x,lazy (intsfrom (x+1)));;

If you know any www/book or anything on this kind of streams please mail me (err...@tlen.pl).
Many thanks.

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

Jonathan Roewen

unread,
Aug 10, 2006, 7:46:04 AM8/10/06
to Error404
The Stream module provides exactly this functionality.

With camlp4o, you can use the old parser syntax too.

#load "camlp4o.cma";

let rec enum_from n = [< 'n; enum_from (n + 1) >];;

let first_10 = Stream.npeek 10 (enum_from 1);;

Streams are also lazily evaluated. More complex examples can be had
too, and there is the parser keyword to deconstruct streams as well.
You should be able to find more info in the camlp4 manual at the caml
site.

Jonathan

Martin Jambon

unread,
Aug 10, 2006, 2:36:19 PM8/10/06
to Error404
On Thu, 10 Aug 2006, Error404 wrote:

> Hi,
>
> I'm looking for some streams related tutorial or any other info.
> By stream i mean something like this (I don't know exact definition):
>
> open Lazy;;
> type 'a stream = Nil | Cons of 'a Lazy.t * 'a stream Lazy.t;;
>
> (* For example stream of 'integers from x' would look like this: *)
>
> let rec intsfrom x =
> Cons(lazy x,lazy (intsfrom (x+1)));;
>
> If you know any www/book or anything on this kind of streams please mail me (err...@tlen.pl).
> Many thanks.

I call this a lazy list. Anyway, I use the following definition:

type 'a l = Empty | Cons of 'a * 'a t
and 'a t = 'a l lazy_t (* only one lazy object per cell *)

See attachment for the full implementation.

You can manipulate such lists like real lists, only the syntax is less
comfortable. They are different from streams in the sense of the standard
Stream module, which are mutable.


Martin

--
Martin Jambon, PhD
http://martin.jambon.free.fr

lizt.ml

Chris King

unread,
Aug 10, 2006, 3:05:14 PM8/10/06
to Jonathan Roewen
On 8/10/06, Jonathan Roewen <jonatha...@gmail.com> wrote:
> The Stream module provides exactly this functionality.

Don't forget about Fstream
(http://caml.inria.fr/pub/docs/manual-camlp4/manual008.html#toc30); it
does the same thing as Stream but in a functional manner (and with a
similar syntax, pa_fstream.cmo).

Jon Harrop

unread,
Aug 10, 2006, 8:05:39 PM8/10/06
to caml...@yquem.inria.fr
On Thursday 10 August 2006 11:51, Error404 wrote:
> I'm looking for some streams related tutorial or any other info.
> By stream i mean something like this (I don't know exact definition):
>
> open Lazy;;
> type 'a stream = Nil | Cons of 'a Lazy.t * 'a stream Lazy.t;;
>
> (* For example stream of 'integers from x' would look like this: *)
>
> let rec intsfrom x =
> Cons(lazy x,lazy (intsfrom (x+1)));;
>
> If you know any www/book or anything on this kind of streams please mail me
> (err...@tlen.pl). Many thanks.

I just posted a link to some stuff on streams in the context of parsing but
you can write intsfrom as:

# #load "camlp4o.cma";;
Camlp4 Parsing version 3.09.2

# let rec intsfrom n = [< 'n; intsfrom(n+1) >];;

--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
Objective CAML for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists

0 new messages