import Prelude hiding (zip,unzip,last,lines,take) -- Zip two lists into a list of pairs: zip :: [a] -> [b] -> [(a,b)] zip [] _ = [] zip _ [] = [] zip (x:xs) (y:ys) = (x,y) : zip xs ys -- Unzip a list of pairs into two lists: unzip :: [(a,b)] -> ([a],[b]) unzip [] = ([],[]) unzip ((x,y):ps) = let (xs,ys) = unzip ps in (x:xs, y:ys) -- Computes/returns the last element of a list: last :: [a] -> Maybe a last [] = Nothing last (x:xs@(_:_)) = last xs last [x] = Just x -- Breaks a string into a list of its lines. lines :: String -> [String] lines "" = [] lines ('\n':cs) = "" : lines cs lines (c:cs) = case lines cs of [] -> [[c]] (l:ls) -> (c:l) : ls -- Factorial function: fac :: Integer -> Integer fac n | n==0 = 1 | otherwise = n * fac (n-1) -- Computes a maximal prefix of a list of a given length (first argument): take :: Int -> [a] -> [a] take n zs | n==0 = [] | otherwise = case zs of [] -> [] (x:xs) -> x : take (n-1) xs