Passing functions to other functions is only half the power of first class functions. Functions may also act as factories , and produce new functions as their results. The ability to create functions with arbitrary capabilities within the program machinery can be quite powerful. For example, one might computationally produce a new comparison function that, in turn, was passed to the qsortF function in the previous panel.
Often, a means of creating a function is with lambda notation. Many languages with functional features use the word "lambda" as the name of the operator, but Haskell uses the backlash character (because it looks somewhat similar to the Greek letter, lambda). A lambda notation looks much like a type signature. The arrow indicates that a lambda notation describes a function from one type of thing ( the thing following the backlash) to another type of thing (whatever follows the arrow).
The example factory mkFunc packs a fair amount into a short description. The main thing to notice is that the lambda indicates a function from n to the result. By the type signature, everything is an Int, although type inference would allow a broader type. The form of the function definition is primitive recursive. An empty list produces a result of zero. A non-empty list produces either the result given by its head pair, or the result that would be produced if only its tail is considered (and the tail eventually shrinks to empty by recursion).
Modules and Program Structure
Basic syntax
So far in this tutorial, we have seen quite a bit of Haskell code in an informal way. In this final section, we make explicit some of what we've been doing. In fact, Haskell's syntax is extremely intuitive and straightforward. The simplest rule is usually to "write what you mean."
Haskell and literate Haskell
The examples in this tutorial have used the standard Haskell format. In the standard format, comments are indicated with a double dash to their left. All comments in the examples are end-of-line comments, which means that everything following a double dash on a line is a comment. You may also create multi-line comments by enclosing blocks in the pair "{-"and"-}". Standard Haskell files should be named with .hs extension.
Literate scripting is an alternative format for Haskell source files. In files named with Literate scripting is an alternative format for Haskell source files. In files named with the .lhs extension, all program lines begin with the greater than character. Everything that is not a program line is a comment. This style places an emphasis on program description over program implementation: It looks something like:
The offside rule
Sometimes in Haskell programs, function definitions will span multiple lines and consist of multiple elements. The rule for blocks of elements at the same conceptual level is that they should be indented more. As soon as an outdent occurs, further lines are promoted back up a conceptual level. In practice, it is obvious, and Haskell will almost always complain on errors.