← | ↑ | → |
Definitions have the following forms.
def x = E end
This definition indicates that x names the value of expression E.
def A B = E end
This is equivalent to
def A = B -> E end.This only makes sense if B is an identifier, but it does not require A to be an identifier. You can use this rule more than once.
For example, you can perform conversions.
def x y z = a end |
⇒ def x y = z -> a end |
⇒ def x = y -> z -> a end |
= def x = y -> (z -> a) end |
The last step follows from the fact that -> associates to the right.
A numberDefinition def twenty = 20 end defines identifier twenty to name number 20. |
A squaring functionFunction sqr returns the square of its parameter. You can define it by def sqr = x -> x*x endor by def sqr x = x*x end |
Maximum
def max x y = case x > y => x | else => y end end |
List concatenationThe following is a definition of the list concatenation function, cat. For example, expression (cat [2,4] [6,8]) yields result [2,4,6,8]. def cat x y = case isNull x => y | else => head x : cat (tail x) y end end After doing replacements, the definition of cat is equivalent to the following. def cat = x -> y -> case isNull x => y | else => head x : cat (tail x) y end end |
You can understand cat by looking at an
cat(a:b:[], c:d:[]) = a : cat(b:[]) (c:d:[]) = a : b : cat([]) (c:d:[]) = a : b : c : d : []
Function compositionHere is a function composition function. (compose f g) is a function which, when applied to x, produces f(g(x)). def compose f g x = f(g x) end You can use function composition to build a function. def fourthPower x = (compose sqr sqr) x end You can also create a function in a more direct way. def fourthPower = (compose sqr sqr) end |
An actionFunction showSquare, below, ignores its parameter. It yields an action that, when run, will read an integer and print the square of that integer. def showSquare a = readInt ~> (x -> print (x*x)) end |
HelloIdentifier main names an action that,
when run, prints
def main = printList ['H','e','l','l','o','\n'] end |