Limbo supports references to functions, similar to closures, but more similar to function pointers.
The example in this section is a derivative of the function reference summary in the Limbo Addendum.
The variable fp is a name whose type is a reference to a function with a given argument and return value scheme. A function which fulfills this scheme can be used to set the reference, in this case the cmp() function is referenced by fp.
The sort() function takes a function reference as an argument, using it as a comparator. In this case cmp() and rcmp() are passed as a function reference argument.
The choose() function returns a reference to a function, being called in this case to return a given comparator with the comparator being called in-line.
; limbo funcrefs.b
; funcrefs
Matched smiley!
Did not match duck
[ d b c a]
[ a b c d]
[ d c b a]
quack ≥ quack
;
- Change the value of
cto 1, what happens? - Write a function which returns a function which returns a function.