(*
Wojciech Muła, $Id: mylist2.ml,v 1.4 2006-06-03 20:56:12 wojtek Exp $
*)
let rec intersparse y = function
| [] -> []
| [x] -> [x]
| x::xs -> x::y::intersparse y xs
let rec dup x n =
if n > 0 then
x::dup x (n-1)
else
[]
let rec takeWhile pred = function
| [] -> []
| x::xs -> if pred x then
x::takeWhile pred xs
else
[]
let rec dropWhile pred = function
| [] -> []
| x::xs as l' -> if pred x then
dropWhile pred xs
else
l'
let group equal l =
let rec aux prev s l =
match l with
[] -> [s]
| x::xs as l' -> if equal x prev then
aux x (x::s) xs
else
s::aux x [] l'
in
match l with
[] -> [[]]
| x::xs -> aux x [x] xs
let inits l =
let rec aux s l =
match l with
[] -> [s]
| x::xs -> s :: aux (s @ [x]) xs
in
aux [] l
let tails l =
let rec aux s l =
match l with
[] -> [s]
| x::xs -> let s' = s @ [x] in
s' :: (aux s' xs)
in
List.rev ([] :: (aux [] l))
let rec isPrefixOf equal l1 l2 =
match l1 with
[] -> true
| h1::t1 -> match l2 with
[] -> false
| h2::t2 -> equal h1 h2 && isPrefixOf equal t1 t2
let isSuffixOf equal l1 l2 =
isPrefixOf equal (List.rev l1) (List.rev l2)
let elemIndex pred y l =
let rec aux i l =
match l with
[] -> None
| x::xs -> if pred x y then
Some i
else
aux (i+1) xs
in
aux 0 l
let elemIndices pred y l =
let rec aux i l =
match l with
[] -> []
| x::xs -> if pred x y then
i::aux (i+1) xs
else
aux (i+1) xs
in
aux 0 l
let rec nub pred = function
| [] -> []
| x::xs -> x::nub pred (List.filter (fun y -> not (pred x y)) xs)
let rec delete pred l =
match l with
[] -> []
| x::xs -> if pred x then
xs
else
x::delete pred xs
let deleteElem y l = delete (fun x -> x=y) l