Skip to content

Instantly share code, notes, and snippets.

@fronx
Created June 23, 2014 21:49
Show Gist options
  • Select an option

  • Save fronx/0fe9124f5e1236bc84ff to your computer and use it in GitHub Desktop.

Select an option

Save fronx/0fe9124f5e1236bc84ff to your computer and use it in GitHub Desktop.

Revisions

  1. fronx created this gist Jun 23, 2014.
    21 changes: 21 additions & 0 deletions NestedLists.hs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    module NestedLists where

    data NestedList a = Nest [NestedList a] | Item a
    deriving Show

    nestedList = Nest [ Item 1
    , Item 2
    , Nest [ Item 3
    , Nest [ Item 4
    , Item 5
    ]
    , Item 6
    ]
    ]

    instance Functor NestedList where
    fmap f (Item x) = Item (f x)
    fmap f (Nest nest) = Nest ((fmap . fmap) f nest)

    main = do
    print $ fmap (*2) nestedList