I would like to implement analog of Haskell cycle
function.
If I pass list elements explicitly it seems trivial:
let cycle a b c =
let rec
You can define it like so also
# let cycle items =
let buf = ref [] in
let rec next i =
if !buf = [] then buf := items;
match !buf with
| h :: t -> (buf := t; Some h)
| [] -> None in
Stream.from next;;
val cycle : 'a list -> 'a Stream.t =
utop # let test = cycle [1; 2; 3];;
val test : int Stream.t =
utop # Stream.npeek 10 test;;
- : int list = [1; 2; 3; 1; 2; 3; 1; 2; 3; 1]
This is from:
http://ocaml.org/tutorials/streams.html