September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,24 +1,26 @@
|
|||
import Data.Tree
|
||||
import Data.Tree (Tree(..), flatten)
|
||||
|
||||
-- [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]
|
||||
-- implemented as multiway tree:
|
||||
|
||||
-- Data.Tree represents trees where nodes have values too, unlike the trees in our problem.
|
||||
-- so we use a list as that value, where a node will have an empty list value,
|
||||
-- and a leaf will have a one-element list value and no subtrees
|
||||
list :: Tree [Int]
|
||||
list = Node [] [
|
||||
Node [] [Node [1] []],
|
||||
Node [2] [],
|
||||
Node [] [
|
||||
Node [] [ Node [3] [],Node [4] []],
|
||||
Node [5] []
|
||||
],
|
||||
Node [] [Node [] [Node [] []]],
|
||||
Node [] [Node [] [Node [6] []]],
|
||||
Node [7] [],
|
||||
Node [8] [],
|
||||
Node [] []
|
||||
]
|
||||
list =
|
||||
Node
|
||||
[]
|
||||
[ Node [] [Node [1] []]
|
||||
, Node [2] []
|
||||
, Node [] [Node [] [Node [3] [], Node [4] []], Node [5] []]
|
||||
, Node [] [Node [] [Node [] []]]
|
||||
, Node [] [Node [] [Node [6] []]]
|
||||
, Node [7] []
|
||||
, Node [8] []
|
||||
, Node [] []
|
||||
]
|
||||
|
||||
flattenList = concat.flatten
|
||||
flattenList :: Tree [a] -> [a]
|
||||
flattenList = concat . flatten
|
||||
|
||||
main :: IO ()
|
||||
main = print $ flattenList list
|
||||
|
|
|
|||
|
|
@ -1,16 +1,22 @@
|
|||
data Tree a = Leaf a | Node [Tree a]
|
||||
data Tree a
|
||||
= Leaf a
|
||||
| Node [Tree a]
|
||||
|
||||
flatten :: Tree a -> [a]
|
||||
flatten (Leaf x) = [x]
|
||||
flatten (Node xs) = concatMap flatten xs
|
||||
|
||||
main = print $ flatten $ Node [Node [Leaf 1],
|
||||
Leaf 2,
|
||||
Node [Node [Leaf 3, Leaf 4], Leaf 5],
|
||||
Node [Node [Node []]],
|
||||
Node [Node [Node [Leaf 6]]],
|
||||
Leaf 7,
|
||||
Leaf 8,
|
||||
Node []]
|
||||
|
||||
main :: IO ()
|
||||
main =
|
||||
(print . flatten) $
|
||||
Node
|
||||
[ Node [Leaf 1]
|
||||
, Leaf 2
|
||||
, Node [Node [Leaf 3, Leaf 4], Leaf 5]
|
||||
, Node [Node [Node []]]
|
||||
, Node [Node [Node [Leaf 6]]]
|
||||
, Leaf 7
|
||||
, Leaf 8
|
||||
, Node []
|
||||
]
|
||||
-- output: [1,2,3,4,5,6,7,8]
|
||||
|
|
|
|||
|
|
@ -1,17 +1,29 @@
|
|||
data NestedList a = NList [NestedList a] | Entry a
|
||||
data NestedList a
|
||||
= NList [NestedList a]
|
||||
| Entry a
|
||||
|
||||
flatten :: NestedList a -> [a]
|
||||
flatten nl = flatten' nl []
|
||||
flatten nl = flatten_ nl []
|
||||
where
|
||||
-- By passing through a list which the results will be preprended to we allow efficient lazy evaluation
|
||||
flatten' :: NestedList a -> [a] -> [a]
|
||||
flatten' (Entry a) cont = a:cont
|
||||
flatten' (NList entries) cont = foldr flatten' cont entries
|
||||
flatten_ :: NestedList a -> [a] -> [a]
|
||||
flatten_ (Entry a) cont = a : cont
|
||||
flatten_ (NList entries) cont = foldr flatten_ cont entries
|
||||
|
||||
-- By passing through a list to which the results will be prepended,
|
||||
-- we allow for efficient lazy evaluation
|
||||
example :: NestedList Int
|
||||
example = NList [ NList [Entry 1], Entry 2, NList [NList [Entry 3, Entry 4], Entry 5], NList [NList [NList []]], NList [ NList [ NList [Entry 6]]], Entry 7, Entry 8, NList []]
|
||||
example =
|
||||
NList
|
||||
[ NList [Entry 1]
|
||||
, Entry 2
|
||||
, NList [NList [Entry 3, Entry 4], Entry 5]
|
||||
, NList [NList [NList []]]
|
||||
, NList [NList [NList [Entry 6]]]
|
||||
, Entry 7
|
||||
, Entry 8
|
||||
, NList []
|
||||
]
|
||||
|
||||
main :: IO ()
|
||||
main = print $ flatten example
|
||||
|
||||
-- output [1,2,3,4,5,6,7,8]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue