1

My problem is, I would like to change every lowercase letter of the list ["hello","wHatS", "up?"] into capitals.

map toUpper [x] does not work realy...

it should return ["HELLO", "WHATS", "UP?"]..

AJF
  • 11,767
  • 2
  • 37
  • 64
satubus
  • 27
  • 5

1 Answers1

6

Take a look at type of toUpper, it's Char -> Char, but you have [[Char]]. It means that you have two layers of list functor here, so you should map it twice.

For pedagogical reasons we may use map here, like this:

map (map toUpper) yourList

Parenthesis are important here, we give one argument to map :: (a -> b) -> [a] -> [b] and get another function of type [Char] -> [Char] (just what we need!) because of curring.

Once you learn about functors, you may prefer fmap and <$> for this task:

(toUpper <$>) <$> yourList
Mark Karpov
  • 7,499
  • 2
  • 27
  • 62
  • 3
    btw: I don't think the doulbe `<$>` is preferable - although `map toUpper <$> yourList` might be an option, if readability is concernced ;) – Random Dev May 05 '15 at 11:29
  • 1
    `fmap`then. I don't think `map` is used in modern Haskell. It seems like haskellers prefer powerful abstractions, like functor, monads, traversable, foldable, etc. `fmap` is more abstract since we can map any functor with it, not just lists. – Mark Karpov May 05 '15 at 11:34
  • 7
    I think in this case the abstraction is not really needed - if you start talking to a beginner like this Haskell will be a dead language soon ;) – Random Dev May 05 '15 at 11:38