caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Martin Jambon <martin1977@laposte.net>
To: Error404 <error92@tlen.pl>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Streams
Date: Thu, 10 Aug 2006 11:32:10 -0700 (PDT)	[thread overview]
Message-ID: <Pine.LNX.4.64.0608101101210.27674@localhost> (raw)
In-Reply-To: <5ebb1214.308e7ea.44db0fae.68763@o2.pl>

[-- Attachment #1: Type: TEXT/PLAIN, Size: 975 bytes --]

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 (error92@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

[-- Attachment #2: Type: TEXT/PLAIN, Size: 2295 bytes --]

(* lazy lists *)

type 'a l = Empty | Cons of 'a * 'a t
and 'a t = 'a l lazy_t

let empty = lazy Empty
let is_empty l = Lazy.force l = Empty

let rec force l =
  match Lazy.force l with
      Empty -> ()
    | Cons (hd, tl) -> force tl

let hd l =
  match Lazy.force l with
      Empty -> invalid_arg "Lizt.hd"
    | Cons (x, _) -> x

let tl l =
  match Lazy.force l with
      Empty -> invalid_arg "Lizt.tl"
    | Cons (_, x) -> x

let peek1 l = 
  match Lazy.force l with
      Empty -> None
    | Cons (x, _) -> Some x

let peek2 l =
  match Lazy.force l with
      Empty -> None
    | Cons (x1, l) -> 
	match Lazy.force l with
	    Empty -> None
	  | Cons (x2, l) -> Some (x1, x2)

let peek3 l =
  match Lazy.force l with
      Empty -> None
    | Cons (x1, l) -> 
	match Lazy.force l with
	    Empty -> None
	  | Cons (x2, l) -> 
	      match Lazy.force l with
		  Empty -> None
		| Cons (x3, l) -> Some (x1, x2, x3)


let rec of_list l =
  lazy (match l with
	    [] -> Empty
	  | hd :: tl -> (Cons (hd, of_list tl)))

let rec to_list l =
  match Lazy.force l with
      Empty -> []
    | Cons (hd, tl) -> hd :: to_list tl

let from f =
  let rec make f i =
    lazy (match f i with
	      None -> Empty
	    | Some x -> Cons (x, make f (i+1))) in
  make f 0

let rec append l1 l2 =
  lazy (match Lazy.force l1 with
	    Cons (hd, tl) -> Cons (hd, (append tl l2))
	  | Empty -> Lazy.force l2)

let rec iter f l =
  match Lazy.force l with
      Empty -> ()
    | Cons (hd, tl) -> f hd; iter f tl

let rec map f l =
  lazy (match Lazy.force l with
	    Empty -> Empty
	  | Cons (hd, tl) -> Cons (f hd, map f tl))

let filter f l =
  let rec loop f l =
    match Lazy.force l with
	Empty -> lazy Empty
      | Cons (hd, tl) -> 
	  if f hd then lazy (Cons (hd, loop f tl))
	  else loop f tl in
  lazy (Lazy.force (loop f l))

let optmap f l =
  let rec loop f l =
    match Lazy.force l with
	Empty -> lazy Empty
      | Cons (hd, tl) -> 
	  match f hd with
	      Some y -> lazy (Cons (y, loop f tl))
	    | None -> loop f tl in
  lazy (Lazy.force (loop f l))

let rec fold_left f accu l =
  match Lazy.force l with
      Empty -> accu
    | Cons (hd, tl) -> fold_left f (f accu hd) tl


  parent reply	other threads:[~2006-08-10 18:32 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-08-10 10:51 Streams Error404
2006-08-10 11:40 ` [Caml-list] Streams Jonathan Roewen
2006-08-10 19:02   ` Chris King
2006-08-10 18:32 ` Martin Jambon [this message]
2006-08-11  0:00 ` Jon Harrop
  -- strict thread matches above, loose matches on Subject: below --
2002-07-05 23:05 [Caml-list] Re: generic programming Dave Berry
2002-08-02 14:49 ` [Caml-list] Streams Diego Olivier Fernandez Pons
2002-08-02 15:29   ` Alain Frisch
2002-08-03 14:19     ` Diego Olivier Fernandez Pons
     [not found] <F241eHu7RVLCMUWktUq0000fbc1@hotmail.com>
2002-04-09 18:09 ` [Caml-list] Cannot find Stream Parser Documentation Wolfram Kahl
2002-04-10 11:03   ` [Caml-list] Streams Daniel de Rauglaudre
2002-04-11 14:35     ` Wolfram Kahl
2001-03-27  2:12 [Caml-list] Caml Development Kit Patrick M Doane
2001-03-27  3:15 ` Brian Rogoff
2001-03-27  6:48   ` [Caml-list] Streams Daniel de Rauglaudre
     [not found]     ` <3AC04E85.EE51D597@univ-savoie.fr>
2001-03-27  8:37       ` Daniel de Rauglaudre

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.64.0608101101210.27674@localhost \
    --to=martin1977@laposte.net \
    --cc=caml-list@inria.fr \
    --cc=error92@tlen.pl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).