Counting argument evaluations under three strategies

Functional languages differ in when they evaluate the arguments of a call. Under call-by-value (used by ML and Scheme), every argument is evaluated exactly once before the function body runs. Under call-by-name, the unevaluated argument expression is substituted into the body and evaluated again each time the body uses that parameter. Under call-by-need (the lazy evaluation of Haskell), the argument is evaluated only the first time the body uses it, and the stored value is reused after that.


Consider these pure functions:


haskell
twice x = x + x
first x y = x
pick b x = if b then x + x else 0


Let e stand for the argument expression (10 * 7). For each call, enter the number of times the multiplication 10 * 7 is performed. The literal arguments 4 and False need no work, and two separately written copies of e are separate expressions that do not share a result.

CallCall-by-valueCall-by-nameCall-by-need
twice e
first 4 e
first e e
pick False e

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Functional Languages and Programs Questions: