Skip to main content Accessibility help
×
Hostname: page-component-5c6d5d7d68-wtssw Total loading time: 0 Render date: 2024-08-14T22:26:31.176Z Has data issue: false hasContentIssue false

7 - Efficiency

Published online by Cambridge University Press:  05 November 2014

Richard Bird
Affiliation:
University of Oxford
Get access

Summary

The question of efficiency has been an ever-present undercurrent in recent discussions, and the time has come to bring this important subject to the surface. The best way to achieve efficiency is, of course, to find a decent algorithm for the problem. That leads us into the larger topic of Algorithm Design, which is not the primary focus of this book. Nevertheless we will touch on some fundamental ideas later on. In the present chapter we concentrate on a more basic question: functional programming allows us to construct elegant expressions and definitions, but do we know what it costs to evaluate them? Alan Perlis, a US computer scientist, once inverted Oscar Wilde's definition of a cynic to assert that a functional programmer was someone who knew the value of everything and the cost of nothing.

Lazy evaluation

We said in Chapter 2 that, under lazy evaluation, an expression such as

sqr (sqr (3+4))

where sqr x = x*x, is reduced to its simplest possible form by applying reduction steps from the outside in. That means the definition of the function sqr is installed first, and its argument is evaluated only when needed. The following evaluation sequence follows this prescription, but is not lazy evaluation:

sqr (sqr (3+4))

= sqr (3+4) ⋆ sqr (3+4)

= ((3+4)⋆(3+4)) ⋆ ((3+4)⋆(3+4))

= …

= 2401

The ellipsis in the penultimate line hides no fewer than four evaluations of 3+4 and two of 7⋆7.

Type
Chapter
Information
Publisher: Cambridge University Press
Print publication year: 2014

Access options

Get access to the full version of this content by using one of the access options below. (Log in options will check for institutional or personal access. Content may require purchase if you do not have access.)

Save book to Kindle

To save this book to your Kindle, first ensure coreplatform@cambridge.org is added to your Approved Personal Document E-mail List under your Personal Document Settings on the Manage Your Content and Devices page of your Amazon account. Then enter the ‘name’ part of your Kindle email address below. Find out more about saving to your Kindle.

Note you can select to save to either the @free.kindle.com or @kindle.com variations. ‘@free.kindle.com’ emails are free but can only be saved to your device when it is connected to wi-fi. ‘@kindle.com’ emails can be delivered even when you are not connected to wi-fi, but note that service fees apply.

Find out more about the Kindle Personal Document Service.

  • Efficiency
  • Richard Bird, University of Oxford
  • Book: Thinking Functionally with Haskell
  • Online publication: 05 November 2014
  • Chapter DOI: https://doi.org/10.1017/CBO9781316092415.008
Available formats
×

Save book to Dropbox

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Dropbox.

  • Efficiency
  • Richard Bird, University of Oxford
  • Book: Thinking Functionally with Haskell
  • Online publication: 05 November 2014
  • Chapter DOI: https://doi.org/10.1017/CBO9781316092415.008
Available formats
×

Save book to Google Drive

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Google Drive.

  • Efficiency
  • Richard Bird, University of Oxford
  • Book: Thinking Functionally with Haskell
  • Online publication: 05 November 2014
  • Chapter DOI: https://doi.org/10.1017/CBO9781316092415.008
Available formats
×