# Modules

Basic implementation of a module system including tests.
This commit is contained in:
2025-01-27 16:04:04 -06:00
committed by James Eversole
parent 72291c652d
commit 4a4b09e898
28 changed files with 373 additions and 105 deletions

View File

@ -1,3 +1,7 @@
!module LOT
!import "lib/base.tri" Lib
main = exampleTwo
-- Level Order Traversal of a labelled binary tree
-- Objective: Print each "level" of the tree on a separate line
@ -15,41 +19,41 @@ main = exampleTwo
-- / / \
-- 4 5 6
label = \node : head node
label = \node : Lib.head node
left = (\node : if (emptyList? node)
left = (\node : Lib.if (Lib.emptyList? node)
[]
(if (emptyList? (tail node))
(Lib.if (Lib.emptyList? (Lib.tail node))
[]
(head (tail node))))
(Lib.head (Lib.tail node))))
right = (\node : if (emptyList? node)
right = (\node : Lib.if (Lib.emptyList? node)
[]
(if (emptyList? (tail node))
(Lib.if (Lib.emptyList? (Lib.tail node))
[]
(if (emptyList? (tail (tail node)))
(Lib.if (Lib.emptyList? (Lib.tail (Lib.tail node)))
[]
(head (tail (tail node))))))
(Lib.head (Lib.tail (Lib.tail node))))))
processLevel = y (\self queue : if (emptyList? queue)
processLevel = Lib.y (\self queue : Lib.if (Lib.emptyList? queue)
[]
(pair (map label queue) (self (filter
(\node : not? (emptyList? node))
(lconcat (map left queue) (map right queue))))))
(Lib.pair (Lib.map label queue) (self (Lib.filter
(\node : Lib.not? (Lib.emptyList? node))
(Lib.lconcat (Lib.map left queue) (Lib.map right queue))))))
levelOrderTraversal_ = \a : processLevel (t a t)
toLineString = y (\self levels : if (emptyList? levels)
toLineString = Lib.y (\self levels : Lib.if (Lib.emptyList? levels)
""
(lconcat
(lconcat (map (\x : lconcat x " ") (head levels)) "")
(if (emptyList? (tail levels)) "" (lconcat (t (t 10 t) t) (self (tail levels))))))
(Lib.lconcat
(Lib.lconcat (Lib.map (\x : Lib.lconcat x " ") (Lib.head levels)) "")
(Lib.if (Lib.emptyList? (Lib.tail levels)) "" (Lib.lconcat (t (t 10 t) t) (self (Lib.tail levels))))))
levelOrderToString = \s : toLineString (levelOrderTraversal_ s)
flatten = foldl (\acc x : lconcat acc x) ""
flatten = Lib.foldl (\acc x : Lib.lconcat acc x) ""
levelOrderTraversal = \s : lconcat (t 10 t) (flatten (levelOrderToString s))
levelOrderTraversal = \s : Lib.lconcat (t 10 t) (flatten (levelOrderToString s))
exampleOne = levelOrderTraversal [("1")
[("2") [("4") t t] t]