Let's say I have a list of functions
functions = [f, g, h]
each one with type a -> a
I also have a list of values, say numbers but anything should work here
vals = [1,2,3]
I want to apply each function in functions
to the corresponding value in vals
My first instinct is to use a lambda and zipWith like:
zipWith (\f v -> f v) functions vals
But frankly this looks ugly and not something I'd expect in such a nice language like Haskell. A function application function sounds like the solution. Is there such a thing? Am I missing something and there is a much nicer solution to my problem? I actually ended-up writing this construct for a Project Euler solution. It works, but I don't like it.
Haskell has first-class functions : functions are values just like integers, lists, etc. They can be passed as arguments, assigned names, etc. … val is value of type Int , and half_of is a value of type Float -> Float .
From HaskellWiki. Currying is the process of transforming a function that takes multiple arguments in a tuple as its argument, into a function that takes just a single argument and returns another function which accepts further arguments, one by one, that the original function would receive in the rest of that tuple.
() is very often used as the result of something that has no interesting result. For example, an IO action that is supposed to perform some I/O and terminate without producing a result will typically have type IO () .
Composing functions is a common and useful way to create new functions in Haskell. Haskell composition is based on the idea of function composition in mathematics. In mathematics, if you have two functions f(x) and g(x), you compute their composition as f(g(x)). The expression f(g(x)) first calls g and then calls f.
zipWith ($) f v
$
is function application. The fact that it has particularly low precedence throws people for a loop sometimes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With