Course of Raku / Functional, concurrent, reactive, and web programming / Functional programming / Lambdas and closures / Exercises

An accumulator

Problem

Write a subroutine make-accumulator that returns a closure keeping a running total. Each call adds its argument to the total and returns the new total. Add 10, then add 5, and print the result of the second call.

Example

The program prints:

15

Solution

See the solution

Course navigation

Solution: A multiplier   |   Solution: An accumulator