5

I am a newbie in Haskell and have some problem of defining a function that would convert all small letters to capital and leave the rest intact.

I tried solving this question in my book so far:

capitalise :: String -> String
capitalise xs = [capitalise2 ch| ch<-xs]

capitalise2 :: Char -> Char
capitalise2 ch 
    | isLower ch    = chr (ord ch - 32)
    | otherwise    = ch

I am getting errors:

p3.hs:6:7: Not in scope: `isLower'
p3.hs:6:23: Not in scope: `chr'
p3.hs:6:28: Not in scope: `ord'

Any help would be much appreciated.

C. A. McCann
  • 76,893
  • 19
  • 209
  • 302
Amjad
  • 1,627
  • 5
  • 21
  • 41
  • Be aware that this sort of strategy will not work in certain languages, because some characters actually become more/less characters when making uppercase/lowercase. – singpolyma Sep 26 '12 at 14:46

3 Answers3

17

First, you need to import Data.Char to use those functions it is complaining about.

Right, you are missing the otherwise case in the new function. Try it with an if .. then .. else construct. Experienced Haskellers do not use that construct very much; I would probably do it with a helper function:

capitalize cs = [ toUpper c | c <- cs ]
    where
    toUpper ...

which is pretty much the same as what you already have, the main difference being the scope of the helper function.

See also Data.Char.toUpper.

This may also be a good opportunity to break free of list comprehensions and start to play with higher order functions. Try writing this function with map instead of a list comprehension.

luqui
  • 59,485
  • 12
  • 145
  • 204
7

Has the book explained the standard library yet?

import Data.Char (toUpper)

capitalise  =  map toUpper
Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • 7
    @iPC better `import Data.Char`. If you're using the haskell98 module `Char`, you cannot use any modules from `base` with newer GHCs. – Daniel Fischer Jan 02 '12 at 13:06
2

You need to make isLower part of the expression, instead of using it as a filter.

[if isLower ch then chr (ord ch - 32) else ch | ch <- xs]

Or, move the helper function inside.

capitalise = map capitalise'
             where capitalise' ch
                      | isLower ch = chr (ord ch - 32)
                      | otherwise  = ch
Cat Plus Plus
  • 125,936
  • 27
  • 200
  • 224