Hi,<br><br>I am new to functional and lazy programming languages ( that&#39;s correct, my life has been pretty pathetic so far) and am not able to understand GHC&#39;s 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 reverse) and return the position of first mismatch. This is part of the right-to-left scan of bayer-moore algorithm.. and it is essential for me to 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 -&gt; String -&gt;Int-&gt;(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 +1)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; in if matched then ((x==y),pos) <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else (False,pos)
<br><br><br><br>The behaviour I expected in four scenarios is as below:<br>1.matchReverse &quot;kapilash&quot; &quot;kapilash&quot; 0 &nbsp; &nbsp; --should return (True,0)<br>2.matchReverse &quot;kapilash&quot; &quot;kapilast&quot; 0&nbsp;&nbsp;&nbsp;&nbsp; --should return (False,8)
<br>3.matchReverse str1 str2 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --should return (False,0)<br>4.matchReverse str1 str1 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --should return (True,0)<br><br>where str1 and str2 are defined as below:<br>&nbsp;str1 =&nbsp; replicate 1000 &#39;a&#39;
<br>&nbsp;str2 =&nbsp; &#39;b&#39;:(replicate 999 &#39;a&#39;)<br><br>what confounds me is that it is able to identify the first element of the tuple in ALL the cases.<br>Invoking fst on the each of the four calls instantly returns the expected 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 &#39;snd&#39; of the 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 loop?
<br><br>Many thanks<br>Kapilash<br><br><br>-- <br>I drink I am thunk.