Skip to content

Instantly share code, notes, and snippets.

@NicolasT
Forked from brinchj/gist:2823458
Created May 30, 2012 19:47
Show Gist options
  • Save NicolasT/2838535 to your computer and use it in GitHub Desktop.
Save NicolasT/2838535 to your computer and use it in GitHub Desktop.
module Main where
-- A MoreList is either a More value which contains a list of MoreList's, or
-- an Elem value, which contains a single value of type a
data MoreList a = More [MoreList a]
| Elem a
flatten :: MoreList a -> [a]
flatten (Elem a) = [a]
flatten (More l) = concatMap flatten l
-- printE takes a MoreList and prints all its Elem values,
-- recursing on More values
-- (it needs the (Show a) context to print values of type a)
printE :: Show a => MoreList a -> IO ()
printE = print . flatten
main :: IO ()
main = do
-- Construct list using Elem and More value constructors
-- list is a MoreList and can contain More and Elem values
let list = More
[Elem 1, Elem 2,
More [Elem 3,
More [Elem 4, Elem 5,
More [Elem 6, Elem 7]
],
Elem 8,
More [ Elem 9, Elem 10]
],
Elem 11]
:: MoreList Int -- type of list
-- Apply printE on list
printE list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment