<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>&lt;noteed@gmail.com&gt;</i></b> wrote:<br><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px;"><br>From: minh thu &lt;noteed@gmail.com&gt;<br>Subject: Re: [Haskell-cafe] New to Haskell - List Comprehension Question<br>To: "Steven1990" &lt;stevenyoung1990@msn.com&gt;<br>Cc: haskell-cafe@haskell.org<br>Date: Wednesday, October 7, 2009, 5:20 PM<br><br><div class="plainMail">2009/10/7 Steven1990 &lt;<a ymailto="mailto:stevenyoung1990@msn.com" href="/mc/compose?to=stevenyoung1990@msn.com">stevenyoung1990@msn.com</a>&gt;:<br>&gt;<br>&gt; Hi, I'm currently
 learning Haskell, and I've been trying to work out a<br>&gt; function for the following problem for a couple of days now.<br>&gt;<br>&gt; I want to use a list comprehension method to change the first letter of a<br>&gt; string to upper case, and the rest of the string to lower case.<br>&gt;<br>&gt; Eg: "heLLo" -&gt; "Hello"<br>&gt;<br>&gt; As I'm trying to learn this, I would appreciate hints rather than the<br>&gt; explicit solution if possible? I'm sure I'm close to a solution, I must be<br>&gt; missing something though. Driving me crazy!<br>&gt;<br>&gt; My attempts are something similar to this:<br>&gt;<br>&gt; upperCase :: String -&gt; String<br>&gt; upperCase xs = [toUpper(x):toLower(xs) | x &lt;- xs]<br>&gt;<br>&gt; I think 'toLower' expects a single character rather than the list which is<br>&gt; one place I'm going wrong?<br><br>Hi,<br><br>try to work little things by little things:<br><br>$ ghci<br>Prelude&gt; let f xs = [x:xs | x &lt;-
 xs]<br>Prelude&gt; f "hello"<br>["hhello","ehello","lhello","lhello","ohello"]<br>Prelude&gt;<br><br>Prelude&gt; :m + Data.Char<br>Prelude Data.Char&gt; :t toLower<br>toLower :: Char -&gt; Char<br>Prelude Data.Char&gt; :t toUpper<br>toUpper :: Char -&gt; Char<br>Prelude Data.Char&gt;<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>