<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: inherit;">Try just writing a function that will change ALL the characters to uppercase (or lower case) using list comprehension and then see if you can isolate how to factor out the special case of the first character.<br><br>Michael<br><br>--- On <b>Wed, 10/7/09, minh thu <i><email@example.com></i></b> wrote:<br><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px;"><br>From: minh thu <firstname.lastname@example.org><br>Subject: Re: [Haskell-cafe] New to Haskell - List Comprehension Question<br>To: "Steven1990" <email@example.com><br>Cc: firstname.lastname@example.org<br>Date: Wednesday, October 7, 2009, 5:20 PM<br><br><div class="plainMail">2009/10/7 Steven1990 <<a ymailto="mailto:email@example.com" href="/firstname.lastname@example.org">email@example.com</a>>:<br>><br>> Hi, I'm currently
learning Haskell, and I've been trying to work out a<br>> function for the following problem for a couple of days now.<br>><br>> I want to use a list comprehension method to change the first letter of a<br>> string to upper case, and the rest of the string to lower case.<br>><br>> Eg: "heLLo" -> "Hello"<br>><br>> As I'm trying to learn this, I would appreciate hints rather than the<br>> explicit solution if possible? I'm sure I'm close to a solution, I must be<br>> missing something though. Driving me crazy!<br>><br>> My attempts are something similar to this:<br>><br>> upperCase :: String -> String<br>> upperCase xs = [toUpper(x):toLower(xs) | x <- xs]<br>><br>> I think 'toLower' expects a single character rather than the list which is<br>> one place I'm going wrong?<br><br>Hi,<br><br>try to work little things by little things:<br><br>$ ghci<br>Prelude> let f xs = [x:xs | x <-
xs]<br>Prelude> f "hello"<br>["hhello","ehello","lhello","lhello","ohello"]<br>Prelude><br><br>Prelude> :m + Data.Char<br>Prelude Data.Char> :t toLower<br>toLower :: Char -> Char<br>Prelude Data.Char> :t toUpper<br>toUpper :: Char -> Char<br>Prelude Data.Char><br><br>So xs is the whole list (the "hello" part of each element in the<br>resilt of f "hello") and x gets the value of each character. toLower<br>and toUpper have the same type; indeed toLower expects a single<br>character while you feed it a list.<br><br>The part on the left of the pipe is "executed" for each x drawn from<br>the xs list. This means that if you want to make something specific to<br>the first element of xs, you have to provide more information: the x<br>alone is not enough to know it is the first one or not.<br><br>The easiest way to do that is with pattern matching on the upperCase argument:<br>upperCase (x:xs) = ...<br><br>(Don't forget for the "other"
case, the empty list:<br>upperCase  = ...)<br><br>Cheers,<br>Thu<br>_______________________________________________<br>Haskell-Cafe mailing list<br><a ymailto="mailto:Haskell-Cafe@haskell.org" href="/mc/compose?to=Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br><a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br></div></blockquote></td></tr></table><br>