Files
fz-lang/exl/hard-1.fzs
T

67 lines
1.0 KiB
Plaintext

class Node
{
int value;
Node next;
fn append(int val) Node
{
if (this.next == null)
{
Node tail = new Node;
tail.value = val;
tail.next = null;
this.next = tail;
return this;
}
this.next = this.next.append(val);
return this;
}
fn sum() int
{
if (this.next == null)
return this.value;
return this.value + this.next.sum();
}
fn printList()
{
printline(this.value);
if (this.next != null)
this.next.printList();
}
}
fn createList(int count) Node
{
if (count <= 0)
return null;
Node head = new Node;
head.value = count;
head.next = createList(count - 1);
return head;
}
printline("===== Test 1: factory & recursive escaping =====");
Node list = createList(3);
printline("Elements:");
list.printList();
printline("===== Test 2: mutation & methods =====");
list = list.append(10);
int total = list.sum();
printline("Sum of elements:");
printline(total);
if (total == 16)
printline("Tests passed!");
else
{
printline("Tests failed");
exit(1);
}