Rework module system
Don't require/allow naming a module, instead require that the importer names it. Allow importing into the local scope with the name !Local. Simplify namespacing logic. Updates all tests to reflect these changes.
This commit is contained in:
@ -1,6 +1,4 @@
|
||||
!module LOT
|
||||
|
||||
!import "lib/base.tri" Lib
|
||||
!import "lib/base.tri" !Local
|
||||
|
||||
main = exampleTwo
|
||||
-- Level Order Traversal of a labelled binary tree
|
||||
@ -19,41 +17,41 @@ main = exampleTwo
|
||||
-- / / \
|
||||
-- 4 5 6
|
||||
|
||||
label = \node : Lib.head node
|
||||
label = \node : head node
|
||||
|
||||
left = (\node : Lib.if (Lib.emptyList? node)
|
||||
left = (\node : if (emptyList? node)
|
||||
[]
|
||||
(Lib.if (Lib.emptyList? (Lib.tail node))
|
||||
(if (emptyList? (tail node))
|
||||
[]
|
||||
(Lib.head (Lib.tail node))))
|
||||
(head (tail node))))
|
||||
|
||||
right = (\node : Lib.if (Lib.emptyList? node)
|
||||
right = (\node : if (emptyList? node)
|
||||
[]
|
||||
(Lib.if (Lib.emptyList? (Lib.tail node))
|
||||
(if (emptyList? (tail node))
|
||||
[]
|
||||
(Lib.if (Lib.emptyList? (Lib.tail (Lib.tail node)))
|
||||
(if (emptyList? (tail (tail node)))
|
||||
[]
|
||||
(Lib.head (Lib.tail (Lib.tail node))))))
|
||||
(head (tail (tail node))))))
|
||||
|
||||
processLevel = Lib.y (\self queue : Lib.if (Lib.emptyList? queue)
|
||||
processLevel = y (\self queue : if (emptyList? 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))))))
|
||||
(pair (map label queue) (self (filter
|
||||
(\node : not? (emptyList? node))
|
||||
(lconcat (map left queue) (map right queue))))))
|
||||
|
||||
levelOrderTraversal_ = \a : processLevel (t a t)
|
||||
|
||||
toLineString = Lib.y (\self levels : Lib.if (Lib.emptyList? levels)
|
||||
toLineString = y (\self levels : if (emptyList? 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))))))
|
||||
(lconcat
|
||||
(lconcat (map (\x : lconcat x " ") (head levels)) "")
|
||||
(if (emptyList? (tail levels)) "" (lconcat (t (t 10 t) t) (self (tail levels))))))
|
||||
|
||||
levelOrderToString = \s : toLineString (levelOrderTraversal_ s)
|
||||
|
||||
flatten = Lib.foldl (\acc x : Lib.lconcat acc x) ""
|
||||
flatten = foldl (\acc x : lconcat acc x) ""
|
||||
|
||||
levelOrderTraversal = \s : Lib.lconcat (t 10 t) (flatten (levelOrderToString s))
|
||||
levelOrderTraversal = \s : lconcat (t 10 t) (flatten (levelOrderToString s))
|
||||
|
||||
exampleOne = levelOrderTraversal [("1")
|
||||
[("2") [("4") t t] t]
|
||||
|
Reference in New Issue
Block a user