Exercise 5.5


doubleEach :: [Int] -> [Int]
doubleEach xs = map (\x -> x * 2) xs

doubleEach' (x:xs) = (x * 2) : doubleEach' xs
doubleEach' [] = []

pairAndOne :: [Int] -> [(Int, Int)]
pairAndOne xs = zip xs (map (\x -> x + 1) xs)

pairAndOne' (x:xs) = (x, x+1) : pairAndOne' xs
pairAndOne' [] = []

addEachPair :: [(Int, Int)] -> [Int]
addEachPair xs = map (\(a,b) -> a + b) xs

addEachPair' ((a,b):xs) = (a + b) : addEachPair' xs
addEachPair' [] = []

One response to “Exercise 5.5”

Leave a Reply

Your email address will not be published. Required fields are marked *