import SimplePrelude hiding (last, unzip, lines, take) -- Compute the last element of a list: last :: [a] -> a last [x] = x last (x:xs@(_:_)) = last xs -- Split a list of pairs into the lists of the components: unzip :: [(a,b)] -> ([a], [b]) unzip [] = ([], []) --unzip ((x,y) : xys) = let (xs,ys) = unzip xys -- in (x:xs, y:ys) unzip ((x,y) : xys) = (x:xs, y:ys) where (xs,ys) = unzip xys -- Breaks a string into a list of lines, where each line is terminated -- with a newline \n character and does not contain newline characters 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 :: Int -> Int fac n | n == 0 = 1 | otherwise = n * fac (n - 1) -- Returns prefix of maximal length n of a list. take :: Int -> [a] -> [a] take n xs | n <= 0 = [] | otherwise = case xs of [] -> [] (x:xs) -> x : take (n - 1) xs