I'm currently learning Haskell, and have encountered the term "function application" a couple of times, without really understanding what is meant by it.
Could someone try to explain the meaning of the term in Haskell, or eventually in general?
"Function application" here just means more or less the same thing as "passing an argument to the function". For example, if you have a function f :: Int -> Int
and an x :: Int
then f x :: Int
is an expression where the expression x
is "applied" as an argument to f
*.
There is no real built-in operator for doing function application in Haskell (other than the whitespace that separates a function from its argument). Prelude
exports the ($)
function though, which is (some unusual oddities aside) just function application: i.e. ($) f x
is the same as f x
(this is mostly used for some syntactic tricks, although it occasionally has other uses as well)
*: It's been pointed out to me that people usually think of this in terms of applying a function to an argument rather than an argument to a function, I don't think this makes much difference for understanding the meaning of application here though
f
(the function) is applied to x
(the argument).
Commented
Aug 28, 2018 at 13:19
(+) :: Int -> Int -> Int
. By providing fewer arguments than the function can take - partially applying the function - we can produce a new function f = (42 +) :: Int -> Int
. This new function, defined by partially applying +
, will increment any provided value by 42.
Commented
Aug 28, 2018 at 15:21
It's mostly just standard English usage.
"Function application" is just application specifically of functions. "Application" in this context is just the noun corresponding to the verb "apply"; it means the act or process of applying something.
To "apply something" means to use it. To "apply A to B" means to do something to B using A. So "apply a function" means to use/call the function on something. In Haskell when I write the expression f x
I am applying f
to x
.
Thus "function application" is just a term for the general concept of applying functions. In specific contexts it might be used to talk about:
$
is often explained as "the function application operator", since f $ x = f x
is more-or-less its definitionOr anything related.
"Applying" a function is the same as calling it, by supplying an argument.
-- A function
f :: a -> a
f x = x
-- Application of f
f 100
Another examples:
data C = A Int Int | B String
f1 :: Int -> Int -> C
f1 = A
f2 :: Int -> C
f2 = A 4
f3 = (4,)
f4 = (,5)
f5 = (,)
I let you play with the types of the last ones.
so, A
and B
are also functions, B
is waiting for a String
, and returns a new type, C
. Same concept with (,)
constructor of tuples.
TupleSections
option to be enabled. (,)
is a constructor, not a an ordinary function.