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

Quiz — Higher-order functions

What does the following program print?

sub prefixer($p) {
    sub ($s) { $p ~ $s };
}

my &hi = prefixer('Hi, ');
say hi('Bob');
1Hi, Bob
0Bob
0Hi,
0Hi, Hi,

prefixer('Hi, ') returns a subroutine that remembers the prefix and prepends it to whatever it is given. Stored in &hi and called with 'Bob', it returns Hi, Bob — a returned subroutine works just as well with strings as with numbers.

Course navigation

Returning subroutines   |   Apply twice