Haskell/More on functions

Here are several nice features that make using functions easier.

let and where revisited
As discussed in earlier chapters,  and   are useful in local function definitions. Here,  calls   function:

But what if we never need  anywhere else? Then we could rewrite  using local bindings. We can do that either with a let binding...

... or with a  clause...

... and the difference appears to be just a question of style: Do we prefer the bindings to come before or after the rest of the definition?

However, there is another important difference between  and. The let...in construct is an expression just like if/then/else. In contrast,  clauses are like guards and so are not expressions. Thus,  bindings can be used within complex expressions:

The expression within the outer parentheses is self-contained, and evaluates to the tangent of the square of the logarithm of. Note that the scope of  does not extend beyond the parentheses, so changing the then-branch to

does not work without dropping the parentheses around the.

Despite not being full expressions,  clauses can be incorporated into   expressions:

In this example, the indentation of the where clause sets the scope of the av variable so that it only exists as far as the RGB red green blue case is concerned. Placing it at the same indentation of the cases would make it available for all cases. Here is an example with guards:

Note that since there is one equals sign for each guard there is no place we could put a  expression which would be in scope of all guards in the manner of the   clause. So this is a situation in which  is particularly convenient.

Anonymous Functions - lambdas
Why create a formal name for a function like  when it only exists within another function's definition, never to be used again? Instead, we can make it an anonymous function also known as a "lambda function". Then,  could be defined like this:

The expression in the parentheses is a lambda function. The backslash is used as the nearest ASCII equivalent to the Greek letter lambda (&lambda;). This lambda function takes two arguments,  and , and it evaluates to "x + read str". So, the  presented just above is precisely the same as the one that used   in a let binding.

Lambdas are handy for writing one-off functions to be used with maps, folds and their siblings, especially where the function in question is simple (beware of cramming complicated expressions in a lambda — it can hurt readability).

Since variables are being bound in a lambda expression (to the arguments, just like in a regular function definition), pattern matching can be used in them as well. A trivial example would be redefining  with a lambda:

Note: Since lambdas are a special character in Haskell, the  on its own will be treated as the function and whatever non-space character is next will be the variable for the first argument. It is still good form to put a space between the lambda and the argument as in normal function syntax (especially to make things clearer when a lambda takes more than one argument).

Operators
In Haskell, any function that takes two arguments and has a name consisting entirely of non-alphanumeric characters is considered an operator. The most common examples are the arithmetical ones like addition (+) and subtraction (-). Unlike other functions, operators are normally used infix (written between the two arguments). All operators can also be surrounded with parentheses and then used prefix like other functions:

We can define new operators in the usual way as other functions — just don't use any alphanumeric characters in their names. For example, here's the set-difference definition from :

As the example above shows, operators can be defined infix as well. The same definition written as prefix also works:

Note that the type declarations for operators have no infix version and must be written with the parentheses.

Sections
Sections are a nifty piece of syntactical sugar that can be used with operators. An operator within parentheses and flanked by one of its arguments...

... is a new function in its own right. , for instance, has the type. We can pass sections to other functions, e.g. . For another example, we can add an extra flourish to the   function we wrote back in Lists II:

If you have a "normal" prefix function and want to use it as an operator, simply surround it with backticks:

This is called making the function infix. It's normally done for readability purposes:  reads better than. You can also define functions infix:

But once again notice that the type signature stays with the prefix style.

Sections even work with infix functions:

Of course, remember that you can only make binary functions (that is, those that take two arguments) infix.