Here's one solution:<div><div>isPalindrome xs =</div><div> <span class="Apple-tab-span" style="white-space:pre">        </span>isPalindrome' 0 (length xs)</div><div> <span class="Apple-tab-span" style="white-space:pre">        </span>where isPalindrome' i j = if i == j then True else check i j</div>
<div> check i j = if (xs !! i) == (xs !! (j-1)) then recurse i j else False</div><div> recurse i j = isPalindrome' (i+1) (j-1)</div><div><br></div><br><div class="gmail_quote">On Mon, Feb 16, 2009 at 10:32 AM, Miguel Pignatelli <span dir="ltr"><<a href="mailto:miguel.pignatelli@uv.es">miguel.pignatelli@uv.es</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div style="word-wrap:break-word"><div style="word-wrap:break-word"><div><div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px">
<div><div>Hi all,<div><br></div><div>This is my first post in this forum, I'm pretty new to Haskell (although I have some previous experience in functional programming with OCaml).</div><div><br></div><div>I'm trying to write the typical function that determines if a list is a palindrome.</div>
<div>The typical answer would be something like:</div><div><br></div><div><div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px">isPalindrome xs = xs == (<span style="color:rgb(64, 192, 192)">reverse</span> xs)</div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px"><br></div><div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px">But I find this pretty inefficient (duplication of the list and double of needed comparisons).</div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px">So I tried my own version using just indexes:</div><div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px"><br></div><div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px">
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px">isPalindrome xs =</div><div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px"> <span style="white-space:pre">        </span>isPalindrome' 0 (<span style="color:rgb(64, 192, 192)">length</span> xs)</div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px"> <span style="white-space:pre">        </span><span style="color:rgb(0, 0, 196)">where</span> isPalindrome' i j =</div><div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px">
<span style="color:rgb(0, 0, 196)">if</span> i == j -- line 43</div><div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px"> <span style="color:rgb(0, 0, 196)">then</span> True</div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px"> <span style="color:rgb(0, 0, 196)">else</span></div><div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px">
<span style="white-space:pre">        </span><span style="color:rgb(0, 0, 196)">if</span> (xs !! i) == (xs !! (j-1))</div><div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px"> <span style="white-space:pre">        </span><span style="color:rgb(0, 0, 196)">then</span> isPalindrome' (i+1) (j-1)</div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px"> <span style="white-space:pre">        </span><span style="color:rgb(0, 0, 196)">else</span> False</div><div><br></div><div>But, when trying to load this in ghci it throws the following error:</div>
<div><br></div><div><div>xxx.hs:43:12: parse error (possibly incorrect indentation)</div><div>Failed, modules loaded: none.</div><div>(Line 43 is marked in the code)</div><div><br></div><div>I seems that the definition of isPalindrome' must be in one line. So, this works as expected:</div>
<div><br></div><div><div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px">isPalindrome xs =</div><div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px"> <span style="white-space:pre">        </span>isPalindrome' 0 (<span style="color:rgb(64, 192, 192)">length</span> xs)</div>
<div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px"> <span style="white-space:pre">        </span> <span style="color:rgb(0, 0, 196)">where</span> isPalindrome' i j = <span style="color:rgb(0, 0, 196)">if</span> i == j <span style="color:rgb(0, 0, 196)">then</span> True <span style="color:rgb(0, 0, 196)">else</span> <span style="color:rgb(0, 0, 196)">if</span> (xs !! i) == (xs !! (j-1)) <span style="color:rgb(0, 0, 196)">then</span> isPalindrome' (i+1) (j-1) <span style="color:rgb(0, 0, 196)">else</span> False</div>
<div><br></div></div><div>Is there any way to make the local definition of isPalindrome' more readable?</div><div><br></div><div><div>Any help in understanding this would be appreciated</div><div><br></div><div>Thanks in advance,</div>
<div><br></div><font color="#888888"><div>M;</div><div><br></div></font></div></div></div></div></div></div></div></div></div></div><br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br></div>