Linked list example
This commit is contained in:
parent
5b6191ae38
commit
b10bfe216a
30
tiger-linked-list.tig
Normal file
30
tiger-linked-list.tig
Normal 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
|
Loading…
Reference in New Issue
Block a user