Skip to main content Accessibility help
×
Home
  • Print publication year: 2016
  • Online publication date: May 2018

1 - Introduction

from Part I - Basic Concepts

Summary

In this chapter we set the stage for the rest of the book. We start by reviewing the notion of a function, then introduce the concept of functional programming, summarise the main features of Haskell and its historical background, and conclude with three small examples that give a taste of Haskell.

Functions

In Haskell, a function is a mapping that takes one or more arguments and produces a single result, and is defined using an equation that gives a name for the function, a name for each of its arguments, and a body that specifies how the result can be calculated in terms of the arguments.

For example, a function double that takes a number x as its argument, and produces the result x + x, can be defined by the following equation:

double x = x + x

When a function is applied to actual arguments, the result is obtained by substituting these arguments into the body of the function in place of the argument names. This process may immediately produce a result that cannot be further simplified, such as a number. More commonly, however, the result will be an expression containing other function applications, which must then be processed in the same way to produce the final result.

For example, the result of the application double 3 of the function double to the number 3 can be determined by the following calculation, in which each step is explained by a short comment in curly parentheses:

double 3

= {applying double}

3 + 3

= {applying +}

6

Similarly, the result of the nested application double (double 2) in which the function double is applied twice can be calculated as follows:

double (double 2)

= {applying the inner double}

double (2 + 2)

= {applying +}

double 4

= {applying double}

4 + 4

= {applying +}

8

Alternatively, the same result can also be calculated by starting with the outer application of the function double rather than the inner:

double (double 2)

= {applying the outer double }

double 2 + double 2

= {applying the first double }

(2 + 2) + double 2

= {applying the first + }

4 + double 2

= {applying double }

4 + (2 + 2)

= {applying the second + }

4 + 4

= {applying + }

8

Related content

Powered by UNSILO