import SimplePrelude hiding (fst, snd, zip, unzip, last, lines, take) -- First component of a pair: fst :: (a,b) -> a fst (x,y) = x -- Second component of a pair: snd :: (a,b) -> b snd (x,y) = y -- Combine two lists into one list of pairs. zip :: [a] -> [b] -> [(a,b)] zip [] ys = [] zip xs [] = [] zip (x:xs) (y:ys) = (x,y) : zip xs ys -- Split a list of pairs into two lists of their components. unzip :: [(a,b)] -> ([a],[b]) unzip [] = ([],[]) --unzip ((x,y) : zs) = let (xs,ys) = unzip zs -- in (x:xs, y:ys) unzip ((x,y) : zs) = (x:xs, y:ys) where (xs,ys) = unzip zs -- Computes the last element of a list: last :: [a] -> a last (x : xs@(_:_)) = last xs last [x] = x -- Breaks a string into a list of lines contained in the string. lines :: String -> [String] lines "" = [] lines ('\n':cs) = "" : lines cs lines (c :cs) = case lines cs of [] -> [[c]] (l:ls) -> (c:l) : ls -- Computes the factorial of an integer. fac :: Int -> Int fac n | n == 0 = 1 | otherwise = n * fac (n - 1) -- Computes the n-th Fibonacci number (inefficient exponential version). fib :: Int -> Int fib n | n == 0 = 0 | n == 1 = 1 | otherwise = fib (n-1) + fib (n-2) -- Computes a prefix up to some given length from a list: take :: Int -> [a] -> [a] take n xs | n == 0 = [] | otherwise = case xs of [] -> [] (x:xs) -> x : take (n - 1) xs