Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function application function in Haskell

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.

like image 301
Juan Pablo Santos Avatar asked Jul 31 '13 03:07

Juan Pablo Santos


People also ask

What are function types in Haskell?

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 .

What is a curried function Haskell?

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.

What does () mean in Haskell?

() 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 () .

What is function composition in Haskell?

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.


1 Answers

zipWith ($) f v

$ is function application. The fact that it has particularly low precedence throws people for a loop sometimes.

like image 98
Thomas M. DuBuisson Avatar answered Sep 23 '22 08:09

Thomas M. DuBuisson