Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 442
Shane Avatar asked Jan 04 '12 22:01

Shane


2 Answers

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

like image 113
luqui Avatar answered Sep 19 '22 13:09

luqui


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)
like image 32
hammar Avatar answered Sep 18 '22 13:09

hammar