Linked list example

This commit is contained in:
Jon Michael Aanes 2024-03-10 15:12:06 +01:00
parent 5b6191ae38
commit b10bfe216a
Signed by: Jmaa
SSH Key Fingerprint: SHA256:Ab0GfHGCblESJx7JRE4fj4bFy/KRpeLhi41y4pF3sNA
1 changed files with 30 additions and 0 deletions

30
tiger-linked-list.tig Normal file
View File

@ -0,0 +1,30 @@
let
type int_node = { val: int, next: int_node }
type int_list = { first: int_node }
function list_new (): int_list =
int_list { first = nil }
function list_add (list: int_list, new_int: int) =
list.first := int_node { val = new_int
, next = list.first }
function list_sum (list: int_list) : int =
let function sum_help (node: int_node): int =
if node <> nil
then node.val + sum_help(node.next)
else 0
in sum_help(list.first)
end
var integers := list_new()
in
list_add(integers, 1);
list_add(integers, 4);
list_add(integers, 1);
list_add(integers, 2);
list_add(integers, 1);
for i := 1 to list_sum(integers) do
print(concat(chr(i + 48), ". line of text\n"));
print("The 10th and last line of text!")
end