Course of Raku / Functional, concurrent, reactive, and web programming / Functional programming / Higher-order functions / Exercises

Apply twice

Problem

Write a subroutine twice that takes a subroutine &f and a value $x, and applies &f to $x twice (that is, f(f($x))). Test it by tripling 2 twice.

Example

The program prints:

18

Solution

See the solution

Course navigation

Quiz — Higher-order functions   |   Solution: Apply twice