Commits

camlspotter committed fab18eb

added haskellish functions (w/o test ;-)

  • Participants
  • Parent commits c78cb5d

Comments (0)

Files changed (2)

File lib/xlist.ml

     match f x with
     | Some v -> v :: st
     | None -> st) [] lst)
+
+let take n xs =
+  let rec take_ n st xs =
+    if n = 0 then st
+    else match xs with
+    | [] -> st
+    | x::xs -> take_ (n-1) (x::st) xs
+  in
+  List.rev (take_ n [] xs)
+
+let rec drop n xs =
+  if n = 0 then xs
+  else match xs with
+  | [] -> []
+  | _::xs -> drop (n-1) xs
+
+let span p xs =
+  let rec span_ st = function
+    | [] -> List.rev st, []
+    | x::xs when p x -> span_ (x::st) xs
+    | l -> List.rev st, l
+  in
+  span_ [] xs

File lib/xlist.mli

 val find_opt : ('a -> bool) -> 'a list -> 'a option
 val find_map_opt : ('a -> 'b option) -> 'a list -> 'b option
 val filter_map : ('a -> 'b option) -> 'a list -> 'b list
+
+val take : int -> 'a list -> 'a list
+val drop : int -> 'a list -> 'a list
+val span : ('a -> bool) -> 'a list -> 'a list * 'a list