1. Everything is a block. Blocks may be defined in this way:
Code: Select all
BlockName
{
...blockdefinition...
}
Code: Select all
n: 5. // n is a copy of "5"
myList: List. // myList is a copy of "List"
Code: Select all
Multiply
{
number1: Int, number2: Int. // data section
^number1 * number2. // code section ('^' means 'yield')
}
Code: Select all
>> Multiply 4, 5.
20
Code: Select all
Stack
{
size: Int,
items: List.
Push
{
item.
items append item.
}
Pop
{
. // a single period indicates no data section for the block
^items removeAt (items length).
}
}
>> Stack 10. // sets the stack's "size", doesn't alter "items"
>> Stack Push 7.
>> Stack Push 22.
>> Stack Push 5.
>> Stack Pop
5
>> Stack items
[7; 22]
Code: Select all
Cat: Animal
{
...redefine some old stuff...
...add some new stuff...
}
Code: Select all
>> x: Int.
>> x = 5.
>> x = 6.
>> x = 'a'.
ERROR: 'x' is type 'Int', not type ''a''
>> y: Object. // note: this is the same as just saying 'y.' but more verbose.
>> y = 7.
>> y = "A string!".
>> z: 42.
>> z = 21.
ERROR: 'z' is type '42', not type '21'
>> z = 42.
>> z = "fourty-two".
ERROR: 'z' is type '42', not type '"fourty-two"'
Code: Select all
Factorial
{
{ n: 0. ^1. }
{ n: Int. ^n * Factorial (n - 1). }
}
>> Factorial 5
120
>> Factorial "abc"
ERROR: 'n' is type '0' or type 'Int', not type '"abc"'
Code: Select all
// prints "Hello, world!" as many times as specified from the command line
HelloWorld
{ . PrintLine "Hello, world!". }
Main
{
sysArgs: List.
1 to (List at 1 toInt), HelloWorld.
}
Code: Select all
// gives us the GCD of two numbers a and b
GreatestCommonDivisor
{
{a: 0, b: Int. ^b. }
{a: Int, b: 0. ^a. }
{
a: Int, b: Int.
(a > b) ifTrue
{ ^GreatestCommonDivisor (a - b) b. }
else
{ ^GreatestCommonDivisor (b - a) a. }.
}
}