In our description of `myDrop`

, we have so far focused on surface
features. We need to go deeper and develop a useful mental model of how
function application works. To do this, we’ll first work through a few
simple examples, until we can walk through the evaluation of the
expression `myDrop 2 "abcd"`

.

We’ve talked a lot about substituting an expression for a variable, and we’ll make use of this capability here. Our procedure will involve rewriting expressions over and over, substituting expressions for variables until we reach a final result. This would be a good time to fetch a pencil and paper, so you can follow our descriptions by trying them yourself.

We will begin by looking at the definition of a simple, nonrecursive function:

-- file: ch02/RoundToEven.hs isOdd n = mod n 2 == 1

Here, `mod`

is the standard modulo function. The
first big step to understanding how evaluation works in Haskell is
figuring out the result of evaluating the expression ```
isOdd (1 +
2)
```

.

Before we explain how evaluation proceeds
in Haskell, let us recap the sort of evaluation strategy more familiar
languages use. First, evaluate the subexpression `1 + 2`

,
to give `3`

. Then apply the `isOdd`

function with `n`

bound to `3`

. Finally, evaluate `mod 3 2`

to
give `1`

, and `1 == 1`

to give
`True`

.

In a language that uses
*strict* evaluation, the arguments to a function are evaluated before the
function is applied. Haskell chooses another path:
*nonstrict* evaluation.

In Haskell, the subexpression ...

Start Free Trial

No credit card required