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 -- Zip two lists into one list: zip :: [a] -> [b] -> [(a,b)] zip [] _ = [] zip _ [] = [] zip (x:xs) (y:ys) = (x,y) : zip xs ys -- Unzip a list of pairs: 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 -- Compute the last element of a list: last :: [a] -> a last [x] = x last (x : xs@(_:_)) = last xs -- Breaks a string into a list of lines. 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 function. fac :: Int -> Int fac n | n == 0 = 1 | otherwise = n * fac (n - 1) -- Returns a prefix of a list. take :: Int -> [a] -> [a] take n xs | n <= 0 = [] | otherwise = case xs of [] -> [] (y:ys) -> y : take (n-1) ys