One Liner Functions

To define a simple one-liner function, one can use the arrow to join the input argument and the output value. For example, function $ f(x) = \frac{1-x}{1+x+x^2}$ can be defined by
f = x -> (1 - x) / (1 + x + x^2)
A defined function can be called in the obvious way
f = x -> (1 - x) / (1 + x + x^2)  // define a function
f(-1)  // call the function

A function can have several input arguments. For example

$\displaystyle f(r,\theta) = r(1+\cos(\theta))
$

can be defined by
 f = (r, theta) -> r * (1 + cos(theta));  // define


oz 2009-12-22