diff --git a/exl/hard-1.fzs b/exl/hard-1.fzs new file mode 100644 index 0000000..077d26e --- /dev/null +++ b/exl/hard-1.fzs @@ -0,0 +1,61 @@ +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 ====="); +int total = list.sum(); +printline("Sum of elements:"); +printline(total); + +if (total == 16) + printline("Tests passed!"); +else printline("Tests failed"); +