could you please define the following functions in haskell and test them. 1)Defi
ID: 3592347 • Letter: C
Question
could you please define the following functions in haskell and test them.
1)Define a Haskell function map2 that takes a list of functions and a list of values and returns the list of results of applying each function in the first list to the corresponding value in the second list.
2)Define a Haskell function fmap that takes a value and a list of functions and returns the list of results from applying each function to the argument value. (For example, fmap 3 [((*) 2), ((+) 2)] yields [6,5].)
3)Define a Haskell function composeList that takes a list of functions and composes them into a single function. (Be sure to give the type signature.)
Explanation / Answer
1. -- Haskell function map2 that takes a list of functions and a list of values and returns the list of results of applying each function in the first list to the corresponding value in the second list.
map2 :: [(a->b)] ->[a] ->[b]
map2 funcs args = zipWith (x y -> x $ y ) funcs args
2.-- Haskell function fmap that takes a value and a list of functions and returns the list of results from applying each function to the argument value.
fmap'' :: a -> [(a->b)] -> [b]
fmap'' arg funcs = map2 funcs (repeat arg)
3. -- Haskell function composeList that takes a list of functions and composes them into a single function.
composeList :: [(a->a)] -> (a->a)
composeList funcs = foldr1 (1 a2-> a1.a2) funcs
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.