[Haskell-beginners] Map on Nested Lists

Tim Sears tim at timsears.com
Thu Apr 8 11:58:41 EDT 2010


If you don't mind choosing a different data type (always a good  
question to ask yourself in haskell) you could simply use your own  
nested lists....

data Nested a  = One a | Nest [Nested a]
                  deriving Show

instance Functor Nested where
     fmap f (One x) = One (f x)
     fmap f (Nest xs) = Nest (map (fmap f) xs)

mapN :: (a -> b) -> Nested a -> Nested b
mapN = fmap

test =  fmap (+1) $ Nest [One 1, Nest [ One 2, One 3], One 4 , Nest  
[ Nest [One 6]] ]
--yields test = Nest [One 2,Nest [One 3,One 4],One 5,Nest [Nest [One  
7]]]



On Apr 8, 2010, at 9:05 AM, beginners-request at haskell.org wrote:

> Re: [Haskell-beginners] Map on Nested Lists

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20100408/b6504acc/attachment.html


More information about the Beginners mailing list