All pastes #813817 Raw Edit

k4st

public text v1 · immutable
·published 2007-12-12 20:47 UTC
rendered paste body

A simple variable:
x = 10;


A simple function:
f[x] = x+2


Let's use longer, more descriptive names:
add_two[x] = x+2

This function will add two to x and that will be the result of the function
(what it returns implicitly)


Here's a simple notation for a function with no name. Notice that it is the
same as above, except that there is no name before the []:
[x] = x+2

On its own, the above unnamed function cannot do anything, so let's set it
to a variable:
add_two = ([x]=x+2)

This variable is now equal to the anonymous function, allowing us to access
the function through the name of the variable.

add_two[2] results in 4


It is now apparent that the following are equivalent:
add_two = ([x]=x+2)
add_two[x] = x+2


This is cool, so now let's make a more generalized solution for the problem
of adding two numbers:
adder[x] = ([y]=x+y)

add_two = adder[2];
add_two[2] results in 4.

also consider calling the adder function in this way:

four = adder[2][2]