2009/10/7 Steven1990 <stevenyoung1...@msn.com>: > > Hi, I'm currently learning Haskell, and I've been trying to work out a > function for the following problem for a couple of days now. > > I want to use a list comprehension method to change the first letter of a > string to upper case, and the rest of the string to lower case. > > Eg: "heLLo" -> "Hello" > > As I'm trying to learn this, I would appreciate hints rather than the > explicit solution if possible? I'm sure I'm close to a solution, I must be > missing something though. Driving me crazy! > > My attempts are something similar to this: > > upperCase :: String -> String > upperCase xs = [toUpper(x):toLower(xs) | x <- xs] > > I think 'toLower' expects a single character rather than the list which is > one place I'm going wrong?
Hi, try to work little things by little things: $ ghci Prelude> let f xs = [x:xs | x <- xs] Prelude> f "hello" ["hhello","ehello","lhello","lhello","ohello"] Prelude> Prelude> :m + Data.Char Prelude Data.Char> :t toLower toLower :: Char -> Char Prelude Data.Char> :t toUpper toUpper :: Char -> Char Prelude Data.Char> So xs is the whole list (the "hello" part of each element in the resilt of f "hello") and x gets the value of each character. toLower and toUpper have the same type; indeed toLower expects a single character while you feed it a list. The part on the left of the pipe is "executed" for each x drawn from the xs list. This means that if you want to make something specific to the first element of xs, you have to provide more information: the x alone is not enough to know it is the first one or not. The easiest way to do that is with pattern matching on the upperCase argument: upperCase (x:xs) = ... (Don't forget for the "other" case, the empty list: upperCase [] = ...) Cheers, Thu _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe