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');| 1 | Hi, Bob |
| 0 | Bob |
| 0 | Hi, |
| 0 | Hi, 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.