
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@haskell.org wrote:
Re: [Haskell-beginners] Map on Nested Lists

On Thu, Apr 8, 2010 at 12:58 PM, Tim Sears
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
Or, if you don't want to allow different "nested-ness": data Nested a = One a | Nest (Nested [a]) Examples of data types: One a :: Nested a Nest (One [a,b,c]) :: Nested a Nest (Nest (One [[a,b],[c,d]])) :: Nested a fromList :: [a] -> Nested a fromList = Nest . One fromList2 :: [[a]] -> Nested a fromList2 = Nest . Nest . One The functor instance: instance Functor Nested where fmap f (One a) = One (f a) fmap f (Nest xs) = Nest (fmap (fmap f) xs) Not tested, use at your own risk ;). Cheers, -- Felipe.
participants (2)
-
Felipe Lessa
-
Tim Sears