Skip to content

Instantly share code, notes, and snippets.

@felipexpert
Last active December 19, 2016 01:37
Show Gist options
  • Save felipexpert/e77f3e94df3641fe11dfa302f68b341d to your computer and use it in GitHub Desktop.
Save felipexpert/e77f3e94df3641fe11dfa302f68b341d to your computer and use it in GitHub Desktop.
-- To run, you will need the Haskell Platform
-- $> runhaskell flatten.hs
data Node a = Single a
| Many [Node a]
deriving Show
flatten :: [Node a] -> [a]
flatten [] = []
flatten ((Single v):xs) = v : flatten xs
flatten ((Many xs):xs') = flatten xs ++ flatten xs'
list = [Single 1, Single 2, Many [Single 3, Many [Single 4, Single 5, Many [Single 6], Single 7], Single 8, Single 9], Single 10]
main = do
putStrLn $ "We are going to flatten the following list: " ++ show list
let flattened = flatten list
print flattened
putStrLn "END"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment