AAh! Thanks a ton!<br><br>Hemanth K<br><br><div class="gmail_quote">On Jan 7, 2008 12:10 AM, Rodrigo Queiro <<a href="mailto:overdrigzed@gmail.com">overdrigzed@gmail.com</a>> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
You have used the name 'pos' twice, for both the parameter and the<br>returned value of the recursive call. The reason this results in an<br>infinite loop is that in code like<br><br>let x = x + 1<br><br>Haskell treats both xs to be references to the same thing, so evaluates:
<br>x<br>= x + 1<br>= (x + 1) + 1<br>= ((x + 1) + 1) + 1<br>...<br><br>which results in the infinite loop.<br><div><div></div><div class="Wj3C7c"><br>On 06/01/2008, Sai Hemanth K <<a href="mailto:saihemanth@gmail.com">
saihemanth@gmail.com</a>> wrote:<br>> Hi,<br>><br>> I am new to functional and lazy programming languages ( that's correct, my<br>> life has been pretty pathetic so far) and am not able to understand GHC's
<br>> behaviour for a particular function. Can someone help me please?<br>><br>> I am trying to write a function which would compare two strings (from<br>> reverse) and return the position of first mismatch. This is part of the
<br>> right-to-left scan of bayer-moore algorithm.. and it is essential for me to<br>> do it from reverse.<br>> Since my goal is to learn haskell, I am not using Data.ByteString.<br>><br>> My function is as follows:
<br>><br>> matchReverse :: String -> String ->Int->(Bool,Int)<br>> matchReverse [] [] pos = (True,pos)<br>> matchReverse _ [] pos = (False,pos)<br>> matchReverse [] _ pos = (False,pos)<br>> matchReverse (x:xs) (y:ys) pos = let (matched,pos) = matchReverse xs ys (pos
<br>> +1)<br>> in if matched then<br>> ((x==y),pos)<br>> else (False,pos)<br>><br>><br>><br>> The behaviour I expected in four scenarios is as below:
<br>> 1.matchReverse "kapilash" "kapilash" 0 --should return (True,0)<br>> 2.matchReverse "kapilash" "kapilast" 0 --should return (False,8)<br>> 3.matchReverse str1 str2 0 --should return (False,0)
<br>> 4.matchReverse str1 str1 0 --should return (True,0)<br>><br>> where str1 and str2 are defined as below:<br>> str1 = replicate 1000 'a'<br>> str2 = 'b':(replicate 999 'a')
<br>><br>> what confounds me is that it is able to identify the first element of the<br>> tuple in ALL the cases.<br>> Invoking fst on the each of the four calls instantly returns the expected<br>> value.(even for the cases 3 and 4 where, there are thousand elements)
<br>> But it seems to go into an infinite loop while calculating the 'snd' of the<br>> tuple. Even for strings containing just one element each.<br>> can someone throw some light on this please? Why does it go into an infinite
<br>> loop?<br>><br>> Many thanks<br>> Kapilash<br>><br>><br>> --<br>> I drink I am thunk.<br></div></div>> _______________________________________________<br>> Haskell-Cafe mailing list<br>>
<a href="mailto: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>>
<br>><br></blockquote></div><br><br clear="all"><br>-- <br>I drink I am thunk.