2

The Prelude function map in Haskell applies a function to a list of inputs:

map :: (a -> b) -> [a] -> [b]

If we would like to apply an element to a list of functions, we could do this with recursion:

functionMap :: a -> [a -> b] -> [b]
functionMap element functions = case functions of
  []   -> []
  x:xs -> x element : functionMap element xs

How could we simplify the above recursion with higher order functions, such as foldl, foldr or map, if it is possible?

1
  • What have you tried? Where are you getting lost? Commented May 26, 2022 at 0:44

1 Answer 1

4

You can map over the functions and apply each to element:

functionMap :: a -> [a -> b] -> [b]
functionMap element functions = map (\f -> f element) functions

This can be shortened using the $ operator to:

functionMap :: a -> [a -> b] -> [b]
functionMap element functions = map ($ element) functions

or just:

functionMap :: a -> [a -> b] -> [b]
functionMap element = map ($ element)

(also: functionMap = map . flip id)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.