19.11.2014 Views

The Fortress Language Specification - CiteSeerX

The Fortress Language Specification - CiteSeerX

The Fortress Language Specification - CiteSeerX

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

halve(x : R64) : R64 = x/2<br />

Predictably, 32-bit precision floats are written R32 .<br />

64-bit integers are denoted by the type Z64 . 32-bit integers by the type Z32 , and infinite precision integers by the<br />

type Z .<br />

2.6 Functions in <strong>Fortress</strong><br />

<strong>Fortress</strong> allows recursive, and mutually recursive function definitions. Here is a simple definition of a factorial<br />

function in <strong>Fortress</strong>:<br />

factorial(n) =<br />

if n = 0 then 1<br />

else n factorial(n − 1) end<br />

Note the juxtaposition of parameter n with the recursive call factorial(n − 1) . In <strong>Fortress</strong>, as in mathematics, multiplication<br />

is represented through juxtaposition. By default, two expressions of numeric type that are juxtaposed represent<br />

a multiplication. On the other hand, juxtaposition of an expression with a function type with another expression<br />

to its right represents function application, as in the following example:<br />

sin x<br />

2.6.1 Keyword Parameters<br />

Functions in <strong>Fortress</strong> can be defined to take keyword arguments by providing default values for parameters. In the<br />

following example:<br />

makeColor(red : Z64 = 0,green : Z64 = 0,blue : Z64 = 0) =<br />

if 0 ≤ red ≤ 255 ∧ 0 ≤ green ≤ 255 ∧ 0 ≤ blue ≤ 255<br />

then Color(red,green,blue)<br />

else throw Error end<br />

the function makeColor takes three keyword arguments, all of which default to 0 . If we call it as follows:<br />

makeColor(green = 255)<br />

the arguments red and blue are both given the value 0 .<br />

<strong>The</strong>re are some other aspects of this example worth mentioning. For example, the body of this function consists of an<br />

if expression. <strong>The</strong> test of the if expression checks that all three parameters have values between 0 and 255 . <strong>The</strong><br />

boolean operator ≤ is chained: An expression of the form x ≤ y ≤ z is equivalent to the expression x ≤ y ∧ y ≤ z ,<br />

just as in mathematical notation. <strong>The</strong> then clause provides an example of a constructor call in <strong>Fortress</strong>, and the else<br />

clause shows us an example of a throw expression in <strong>Fortress</strong>.<br />

2.6.2 Varargs Parameters<br />

It is also possible to define functions that take a variable number of arguments. We have already seen such a function:<br />

Executable.run . Here is another:<br />

23

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!