making new functions with optional arguments
Let's consider an example where we wish to construct a linear function of x
called f, with the slope and y-intercept of the graph being optional
arguments of f. We use the @ operator to attach the default
values to our function, coded in a special way.
i1 : f = {Slope => 1, Intercept => 1} @
(opts -> x -> x * opts.Slope + opts.Intercept)
o1 = f
o1 : Function |
i2 : f 5
o2 = 6 |
i3 : f(5,Slope => 100)
o3 = 501 |
i4 : f(5,Slope => 100, Intercept => 1000)
o4 = 1500 |
In the example the function body is the code x * opts.Slope + opts.Intercept.
When it is evaluated, a hash table is assigned to opts; its
keys are the names of the optional arguments, and the values
are the corresponding current values, obtained either from the default values
specified in the definition of f, or from the options specified at
the time f is called.
In the example above, the inner function has just one argument, x,
but handling multiple arguments is just as easy. Here is an example with two
arguments.
i5 : f = {a => 1000} @ (o -> (x,y) -> x * o.a + y); |
i6 : f(3,7)
o6 = 3007 |
i7 : f(5,11,a=>10^20)
o7 = 500000000000000000011 |



