<div dir="ltr"><div>Here's why you're getting the longer repetition first:</div><div><br></div>buildLenList "bang"<div>= buildLenList (_:"ang")</div><div>= [1 + length "ang"] ++ buildLenList "ang"</div><div>= [4] ++ buildLenList "ang"</div><div>(...) = [4, 3, 2, 1]</div><div><br></div><div>And the reason calling myReverse on that doesn't give you [1, 2, 3, 4] is that you're reversing a different list every time:</div><div><br></div><div>blowup "bang"</div><div>= blowup ('b':"ang")</div><div>= <span style="font-size:12.8px">myRepeat 'b' (head (myReverse (buildLenList ('b':"ang"))) ++ blowup "ang"</span></div><div><span style="font-size:12.8px">= myRepeat 'b' (head (myReverse [4, 3, 2, 1]) ++ blowup "ang"</span></div><div><span style="font-size:12.8px">= myRepeat 'b' (head [1, 2, 3, 4] ++ blowup "ang"</span></div><div><span style="font-size:12.8px">= myRepeat 'b' 1 ++ blowup "ang"</span></div><div><span style="font-size:12.8px">= "b" ++ blowup "ang"</span></div><div><span style="font-size:12.8px">= "b" ++ </span>blowup ('a':"ng")</div><div><div>= "b" ++ <span style="font-size:12.8px">myRepeat 'a' (head (myReverse (buildLenList ('a':"ng"))) ++ blowup "ng"</span></div></div><div><span style="font-size:12.8px">= "b" ++ myRepeat 'a' </span><span style="font-size:12.8px">(head (myReverse [3, 2, 1]) ++ blowup "ng"</span></div><div><div><span style="font-size:12.8px">= </span><span style="font-size:12.8px">"b" ++</span><span style="font-size:12.8px"> </span><span style="font-size:12.8px">myRepeat 'a' (head [1, 2, 3] ++ blowup "ng"</span></div></div><div><span style="font-size:12.8px">(...)</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">Since you only asked for help with how it evaluates, I'll withhold further spoilers. :) However, I strongly recommend getting more comfortable using the (:) operator for construction instead of just pattern matching; in a couple of your functions it would be a better tool than (++). Also, "at-patterns" are great:</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">blowup l@(x:xs) = myRepeat x (head (buildLenList l)) ++ blowup xs</span><span style="font-size:12.8px"><br></span></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Jan 9, 2016 at 7:21 AM, Fabien R <span dir="ltr"><<a href="mailto:theedge456@free.fr" target="_blank">theedge456@free.fr</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello,<br>
<br>
I want to define a function blowup that takes "bang" as input and returns "baannngggg".<br>
I come up with these functions;<br>
<br>
myReverse :: [a] -> [a]<br>
myReverse [] = []<br>
myReverse (x:xs) = myReverse xs ++ [x]<br>
<br>
buildLenList :: String -> [Int]<br>
buildLenList "" = []<br>
buildLenList (_:xs) = [1 + length xs ] ++ buildLenList xs<br>
<br>
myRepeat :: Char -> Int -> String<br>
myRepeat x 0 = []<br>
myRepeat x n = [x] ++ myRepeat x (n - 1)<br>
<br>
blowup :: String -> String<br>
blowup [] = []<br>
blowup (x:xs) = myRepeat x (head ( (buildLenList (x:xs)))) ++ blowup xs<br>
<br>
With this code, blowup "bang" returns "bbbbaaanng".<br>
<br>
So I thought to insert myReverse between head and buildLenList but in that case, the result is only "bang".<br>
<br>
It seems that the evaluation of buildLenList is not working as I thought. I tried to debug that using ghci debugger but failed (still learning).<br>
Can someone explain how the evaluation is done here ?<br>
<br>
--<br>
Fabien<br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
</blockquote></div><br></div>