Skip to content

Simple Functions

Elementary functions can often be defined directly:

\[\begin{gathered} f : \mathbb{Z} \rightarrow \mathbb{Z}\\ f(x) = 2 \cdot x \end{gathered}\]

We can do the same in Haskell:

f :: Int -> Int
f x = 2 * x

The notation here deviates from the notation used in (most of) math and other programming languages. Similar to shell commands, function arguments are separated from the function by one or more spaces. They are not enclosed in parentheses. Apart from that, this Haskell definition of \(f\) is a pretty verbatim translation of the mathematical equation.

Coming from Java, Python or any other imperative programming language, you may notice the conspicuous absence of a return statement. That's no accident. Remember, we are not defining a sequence of steps to be executed; we are defining the value of a function, given some argument. In Java, Python or C, it makes sense to say, do this, do that, and then return this value. In Haskell, we say, the value of f applied to some argument x is 2 * x, expressed using the equal sign (=).