Skip to content

Instantly share code, notes, and snippets.

@brinchj
Created May 29, 2012 09:08
Show Gist options
  • Save brinchj/2823458 to your computer and use it in GitHub Desktop.
Save brinchj/2823458 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
-- 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 (Elem a) = print a -- single values (Elem) are just printed
printE (More l) = mapM_ printE l -- apply printE to all values inside More
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