Learning a functional programming language requires you to think in completely new ways. I guess the majority of people start with imperative languages (“normal” programming) and only dive into the functional world after some time. For me personally it has been close to four years since I started programming and I only began learning Haskell a few weeks ago. Apart from the use of map, filter and reduce in JavaScript I had no idea what this the whole thing is about. That being said, I am still a beginner but starting to have some eye-opener moments every now and then. This post is about function arguments in Haskell and how the concepts of function and argument are much more flexible than in imperative programming or in mathematical functions. To illustrate the aha-moments I had, I will simply go through some equivalent ways of writing the addition function + for integers. None of this code is of any use and the function is trivial on purpose. The point is to illustrate many different ways to think and write the same thing.

f :: Int -> Int -> Int
f x y = x + y

This is a trivial and useless definition, but please bare with me. The mental exercise here is not about the function itself, but its notation. If you think about what the value of f x y is, you might say something like f is the same as addition”. Well, in Haskell this translates directly to code:

f = (+)

This is literally the same definition as before except that now arguments are implicit. We could also include them:

f x y = (+) x y

Now for a slightly weirder version we can include just one of them:

f x = (+) x

This makes use of a partial application1 which results in a new function that takes the remaining arguments. If you look closely at the type definition of f, it actually says that it maps an Int to a function Int -> Int. Here we say that given the first argument x it should return (+) x which is indeed a function Int -> Int. So the types match and this notation shows the fact that f takes only one argument, even though its definition might suggest otherwise.

Can you type a lambda?

Can you? A lambda is this greek letter . Haskell makes typing it easy: \. Seriously, a lambda in Haskell is an anonymous function which simply means that it has no name. They are also expressions which means you can put them basically everywhere. The syntax is this:

-- A function that maps x to x + 4
\x -> x + 4

Remember how I said that functions with multiple arguments are actually multiple functions with one argument each? We can translate this into code with lambdas:

-- We know that there are actually two functions here
f x y = x + y

-- These two
f x = \y -> x + y

And again this is literally the same thing as every other definition of f before in this post. I hope these examples help or motivate you to explore the concept of functions, arguments and partial application. They are ordered in the way that they started to make sense to me, but that is not likely to be the same order for you.