File: lazyList.ml

package info (click to toggle)
xen-api-libs 0.5.2-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,940 kB
  • sloc: ml: 13,925; sh: 2,930; ansic: 1,699; makefile: 1,240; python: 83
file content (20 lines) | stat: -rw-r--r-- 457 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
(* A lazy-list implementation *)

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

let rec map f xs = lazy(match Lazy.force xs with
	| Empty -> Empty
	| Cons(x, xs) -> Cons(f x, map f xs))
	
let rec take n xs = lazy(match n, Lazy.force xs with
	| 0, _ -> Empty
	| n, Empty -> raise Not_found
	| n, Cons(x, xs) -> Cons(x, take (n - 1) xs)) 
	
let rec iter f xs = match Lazy.force xs with
	| Empty -> ()
	| Cons(x, xs) -> f x; iter f xs