It is possible to return functions as value. This way you can build functions which construct special purpose functions according to some parameters. The tricky bit is what variables does the function see. The way this works in GEL is that when a function returns another function, all identifiers referenced in the function body are prepended a private dictionary of the returned function. So the function will see all variables that were in scope when it was defined. For example we define a function which returns a function which adds 5 to its argument.
function f() = ( k = 5; `(x) = (x+k) )Notice that the function adds
k
to
x
. You could use this as follows.
g = f(); g(5)And g(5) should return 10.
One thing to note is that the value of k
that is used is the one that's in effect when the
f
returns. So for example
function f() = ( k = 5; r = `(x) = (x+k); k = 10; r )will return a function that adds 10 to its argument rather than 5. This is because the extra dictionary is created only when the context in which the function was defined ends, which is when the function
f
returns. This is consistent with how you
would expect the function r
to work inside
the function f
according to the rules of
scope of variables in GEL. Only those variables are added to the
extra dictionary that are in the context that just ended and
no longer exists. Variables
used in the function that are in still valid contexts will work
as usual, using the current value of the variable.