Last active
December 19, 2016 01:37
-
-
Save felipexpert/e77f3e94df3641fe11dfa302f68b341d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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