Next: 6.10 The standard libraries Up: 6. Programming Previous: 6.8 Lists and recursion Contents

#
6.9 Lazy programming

`nip2`'s programming language is lazy, that is, it delays evaluation as long as it possibly can. For example, `error` is a function which immediately halts execution of your function and pops up an alert window. So:

> | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> | `12 + error "wombat!"` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |

Has no value: this expression will halt with an error message. However:

> | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> | `false && error "lasagne!"` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |

Will evaluate to `false`, since `nip2` knows after looking at the left-hand-side of `&&` that the result must be `false`, and so does not evaluate the right-hand-side.

> | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> | `[12, error "hot chilli!"] ? 0 == 12` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |

This also evaluates completely, since the second element of the list is never used, and therefore never evaluates.

Things become more confusing when you start calling functions, since the arguments to a function call are also not evaluated until the function needs that value. For example:

> | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> | `foldr (error "boink!") 2 [] == 2` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |

Again, this evaluates successfully, since the function is never used by `foldr`.

Next: 6.10 The standard libraries Up: 6. Programming Previous: 6.8 Lists and recursion Contents