views:

210

answers:

2

What OCaml libraries are out there that provide lazy list handling? I am looking for something along these lines:

type 'a lazy_list = (*'*)
  | Nil
  | Cons of 'a * 'a lazy_list lazy_t

let from f = 
  let rec gen n = 
    lazy 
      (
        match f n with 
          | Some x ->
              Cons (x, gen (n + 1))
          | None ->
              Nil
      )
  in 
    gen 0

Integration with the Stream type and syntactic sugar for backtracking Camlp4 parsers would be nice.

+5  A: 

Ocaml Batteries has a lazy list module, check out the to_stream function. As for backtracking, you can look into camlp4's stream parsers now that you have a Stream.t .

nlucaroni
Thanks, lazy list module is very handy. Not sure about the stream parsers though as they do not backtrack. But I'll figure it out from here.
toyvo
well, camlp4 doesn't backtrack? Maybe I was thinking about camlp5? One of them (or both) does backtracks via continuations...
nlucaroni
Ah yes, that seems be camlp5. Thanks!http://pauillac.inria.fr/~ddr/camlp5/doc/htmlc/bparsers.html
toyvo
+1  A: 

Also, there is a lazy list module called Cf_seq in my OCaml Network Application Environment Core Foundation. In fact, I wrote a whole passle of functional data structures. It's all available under a 2-clause BSD license. Enjoy.

james woodyatt