Commits

camlspotter  committed fad650d

added zlist

  • Participants
  • Parent commits 81efb60

Comments (0)

Files changed (4)

File lib/OMakefile

    phantom
    weaktbl
    hashset
+   zlist
    spot
 
 LIB = spotlib

File lib/xlazy.ml

 
 let (!!) = force
 let eager = Lazy.lazy_from_val
+let from_val = Lazy.lazy_from_val
 
 let peek v = if lazy_is_val v then Some (!!v) else None
       

File lib/xlazy.mli

 (** Same as Lazy.force *)
 
 val eager : 'a -> 'a t
+val from_val : 'a -> 'a t
+
 val peek : 'a t -> 'a option
 val is_val : 'a t -> bool

File lib/zlist.ml

+open Xlazy
+
+type 'a t = 'a desc lazy_t
+
+and 'a desc = 
+  | Cons of 'a * 'a t
+  | Null
+
+let null = from_val Null
+let cons v t = from_val (Cons (v, t))
+let (^:) v t = lazy (Cons (v, t))
+
+let peek = function
+  | lazy Null -> None
+  | lazy (Cons (v, t')) -> Some (v, t')
+
+let is_null = function
+  | lazy Null -> true
+  | _ -> false
+
+let rec of_list = function
+  | [] -> null
+  | x::xs -> x ^: of_list xs
+
+let to_list t =
+  let rec to_list st = function
+    | lazy Null -> List.rev st
+    | lazy (Cons (elem, t)) -> to_list (elem :: st) t
+  in
+  to_list [] t
+
+let rec fold_right f t st =
+  lazy (match t with
+  | lazy Null -> !!st
+  | lazy (Cons (v, t)) -> !!(f v (fold_right f t st)))
+
+let append t1 t2 = fold_right (^:) t1 t2
+
+let hd = function
+  | lazy Null -> failwith "hd"
+  | lazy (Cons (x, _)) -> x
+
+let tl = function
+  | lazy Null -> failwith "tl"
+  | lazy (Cons (_, xs)) -> xs
+
+let rec init t = lazy (match t with
+  | lazy Null -> failwith "init"
+  | lazy (Cons (_, lazy Null)) -> Null
+  | lazy (Cons (x, xs)) -> Cons (x, init xs))
+
+let rec length = function
+  | lazy Null -> 0
+  | lazy (Cons (_, xs)) -> length xs + 1
+
+let map f t = fold_right (fun x st -> f x ^: st) t null
+
+let rec fold_left f st t = lazy (match t with
+  | lazy Null -> !!st
+  | lazy (Cons (v, t)) -> !! (fold_left f (f v st) t))
+
+let rev t = fold_left (^:) null t
+
+let intersparse a t = lazy (match t with
+  | lazy Null -> Null
+  | lazy (Cons (_, lazy Null) as singleton) -> singleton
+  | lazy (Cons (x, xs)) -> Cons (x, from_val (Cons (a, xs)))) 
+
+let rec concat xss = lazy (match xss with
+  | lazy Null -> Null
+  | lazy (Cons (x, xs)) -> !! (append x (concat xs)))
+
+let intercalate xs xss = concat (intersparse xs xss)
+
+(*
+transpose :: [[a]] -> [[a]]Source
+
+The transpose function transposes the rows and columns of its argument. For example,
+
+ transpose [[1,2,3],[4,5,6]] == [[1,4],[2,5],[3,6]]
+subsequences :: [a] -> [[a]]Source
+
+The subsequences function returns the list of all subsequences of the argument.
+
+ subsequences "abc" == ["","a","b","ab","c","ac","bc","abc"]
+permutations :: [a] -> [[a]]
+*)
+
+let rec fold_left' f st = function
+  | lazy Null -> st
+  | lazy (Cons (v, t)) -> fold_left' f (f v st) t
+
+let rec fold_left1 f t = match t with
+  | lazy Null -> failwith "fold_left1"
+  | lazy (Cons (v, t)) -> fold_left f v t
+
+
+
+
+  
+
+