import Prelude hiding (zip, unzip, last, take) -- 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 into two lists. unzip :: [(a,b)] -> ([a], [b]) unzip [] = ([], []) unzip ((x,y):xys) = let (xs,ys) = unzip xys in (x : xs, y : ys) --unzip ((x,y):xys) = let lists = unzip xys -- in (x : fst lists, y : snd lists) -- The last element of a list: last :: [a] -> a last [x] = x last (x:xs@(_:_)) = last xs -- Unzip a list of pairs into two lists. unzipC :: [(a,b)] -> ([a], [b]) unzipC [] = ([], []) unzipC ((x,y):xys) = case unzipC xys of (xs,ys) -> (x : xs, y : ys) -- Add all integer and concatenate all strings in a mixed list. sumConc :: [Either Int String] -> (Int, String) sumConc [] = (0, "") sumConc (x:xs) = case sumConc xs of (sum, cs) -> case x of Left n -> (sum+n, cs) Right s -> (sum , s ++ cs) -- Computes the factorial of a natural number. fac :: Int -> Int fac n | n == 0 = 1 | n > 0 = n * (fac (n - 1)) -- Computes the prefix of a maximal 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