module Cookbook.Essential.Common where
sub :: (Eq a) => [a] -> Int -> [a]
sub [] _ = []
sub x 0 = x
sub (_:xs) c = sub xs (c 1)
positions :: (Eq a) => [a] -> a -> [Int]
positions x c = let y = zip x [0..(length x)] in find y
where find y = [e | (d,e) <- y, d == c]
pos :: (Eq a) => [a] -> a -> Int
pos x c | c `notElem` x = 1
pos x c = let ans = positions x c in ((if (length ans) > 1 then (head . tail) else head) ans)
apply :: [(a -> a)] -> a -> a
apply [] c = c
apply (f:fs) c = apply fs (f c)
flt :: [[a]] -> [a]
flt [] = []
flt (x:xs) = x ++ flt xs
fromLast :: ([a] -> [a]) -> [a] -> [a]
fromLast f c = rev $ f $ rev c
where rev [] = []
rev (x:xs) = rev xs ++ [x]
afterX :: (Eq a) => [a] -> a -> Int -> [a]
afterX (x:xs) c t
| t == 0 = x:xs
| otherwise = if x == c then afterX xs c (pred t) else afterX xs c t
iSp :: [a] -> a -> [a]
iSp [] _ = []
iSp (x:xs) c = ([x] ++ [c]) ++ iSp xs c