8

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?

4 Answers 4

8

"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

7
  • 11
    I would say it's the other way round: f (the function) is applied to x (the argument).
    – melpomene
    Commented Aug 28, 2018 at 13:19
  • @melpomene Ah, apologies I'm not sure what interpretation makes more sense in english.
    – Cubic
    Commented Aug 28, 2018 at 13:22
  • For additional fun @Mr.Eivind might also want to know the term "partial function application". Consider a function that takes more than one argument (+) :: 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
  • 2
    "function application" comes from math and "x is applied to f" is simply incorrect usage (in the same way you don't apply your skin to the lotion), so I'd recommend just changing the answer. Another nice thing to add might be to point out clearly that when talking about "function application" we're speaking about a particular syntactic construct (i.e. function application is something GHC's parser understands) rather than e.g. a property of program evaluation.
    – jberryman
    Commented Aug 28, 2018 at 19:47
  • 1
    Yeah I like "apply f to x" and "pass x to f"
    – luqui
    Commented Aug 29, 2018 at 3:32
5

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:

  1. The broad notion of applying functions in general
  2. The syntax used to express "apply this function to that argument" in a programming language (e.g. "function application is by adjacency in Haskell", or "function application in Python uses C-like syntax")
  3. A specific bit of code that is applying a function
  4. In Haskell $ is often explained as "the function application operator", since f $ x = f x is more-or-less its definition

Or anything related.

4

"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
1
  • 1
    I really like this answer, short and sweet, not a single char is wasted Commented Aug 28, 2018 at 15:33
0

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.

1
  • The last three require the TupleSections option to be enabled. (,) is a constructor, not a an ordinary function.
    – chepner
    Commented Aug 28, 2018 at 19:20

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.