Tracing an accumulator recursion

The pure Haskell function below sums the integers from 1 to n by passing a running total as an argument instead of updating a loop variable.


haskell
sumTo :: Int -> Int -> Int
sumTo acc 0 = acc
sumTo acc n = sumTo (acc + n) (n - 1)


The expression sumTo 0 5 is evaluated. Complete the table with the values of acc and n in each successive call, as integers. Call 1 is given.

Callaccn
Call 105
Call 2
Call 3
Call 4
Call 5
Call 6

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: