Shane
Shane

Reputation: 2375

Double map in haskell?

I am still a haskell beginner. Can I do a double map in haskell?

For example, if I have a [[Char]] and I want to convert each Char in each [Char] to lower case, is there an easy way to do this rather than something like:

exampleF [] = []
exampleF (x:xs) = (map toLower x) : exampleF xs

Upvotes: 19

Views: 10522

Answers (2)

luqui
luqui

Reputation: 60463

In fact, there is a nice pattern here:

map           :: (a -> b) ->   [a]   ->   [b]
(map.map)     :: (a -> b) ->  [[a]]  ->  [[b]]
(map.map.map) :: (a -> b) -> [[[a]]] -> [[[b]]]

and so on

Upvotes: 29

hammar
hammar

Reputation: 139830

You can think of map f, as transforming a function f :: a -> b into a function on lists map f :: [a] -> [b], so if you want to transform it further into a function on lists of lists, you just need to use map again to get map (map f) :: [[a]] -> [[b]].

In this particular case, that becomes:

exampleF = map (map toLower)

Upvotes: 25

Related Questions