RosettaCodeData/Task/Flatten-a-list/Haskell/flatten-a-list-2.hs

24 lines
382 B
Haskell
Raw Permalink Normal View History

2017-09-23 10:01:46 +02:00
data Tree a
= Leaf a
| Node [Tree a]
2013-04-10 21:29:02 -07:00
flatten :: Tree a -> [a]
flatten (Leaf x) = [x]
2019-09-12 10:33:56 -07:00
flatten (Node xs) = xs >>= flatten
2013-04-10 21:29:02 -07:00
2017-09-23 10:01:46 +02:00
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 []
]
2019-09-12 10:33:56 -07:00
-- [1,2,3,4,5,6,7,8]