<div dir="ltr">Note that you would like the elem function to stop at the matching element and return True rather than checking the rest of the list needlessy, which can be done with foldr but not with foldl. The actual implementation of elem does this, so you can say:<div><br></div><div>> elem 1 [1..]</div><div>True</div><div><br></div><div>which would fail to terminate with the foldl version.</div></div><br><div class="gmail_quote"><div dir="ltr">On Fri, Sep 25, 2015 at 11:10 AM goforgit . <<a href="mailto:teztingit@gmail.com">teztingit@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Thanks I got it now :)<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Sep 24, 2015 at 9:45 PM, Kostiantyn Rybnikov <span dir="ltr"><<a href="mailto:k-bx@k-bx.com" target="_blank">k-bx@k-bx.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><p dir="ltr">Hi.</p>
<p dir="ltr">Your function gets passed numbers one by one in the place of x, and its previous result in the place of acc, and it returns a Bool. Initial value in place of acc parameter ("previous result") is put as False (since you begin with answer "no" to question "is it elem?").</p>
<p dir="ltr">Hope this helps.<br>
</p>
<div class="gmail_quote">24 вер. 2015 19:04 "goforgit ." <<a href="mailto:teztingit@gmail.com" target="_blank">teztingit@gmail.com</a>> пише:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div><div dir="ltr"><div><div><div><div><div><div><div>Reading <a href="http://learnyouahaskell.com/higher-order-functions" target="_blank">http://learnyouahaskell.com/higher-order-functions</a><br><br></div>I understand that with the function<br><span><span><br><span style="font-family:monospace,monospace">sum'</span></span><span style="font-family:monospace,monospace"><span> :: </span><span>(</span><span>Num</span><span> a)</span><span> => </span><span>[a]</span><span> -> </span><span>a </span></span></span><span style="font-family:monospace,monospace"><br>sum'<span> = </span><span>foldl (+) </span><span>0</span><span><br></span><span><span></span></span></span><br></div><div>the call<br></div><div><br></div>ghci>>> sum' [1,2,3]<br><br></div>will be evaluated as<br><br>0 + 1 + 2 + 3 = 6<br><br></div>But what about the function <span><span><br><br><span style="font-family:monospace,monospace">elem'</span></span><span style="font-family:monospace,monospace"><span> :: </span><span>(</span><span>Eq</span><span> a)</span><span> => </span><span>a</span><span> -> </span><span>[a]</span><span> -> </span><span>Bool</span><span> <br></span></span></span><span style="font-family:monospace,monospace">elem' y ys<span> = </span><span>foldl (\acc x</span><span> -> </span><span>if</span><span> x</span><span> == </span><span>y </span><span>then</span><span> </span><span>True</span><span> </span><span>else</span><span> acc) </span><span>False</span><span> ys</span></span><br><br></div>and calling it with<br><br></div>ghci>>> elem' 3 [1,2,3]<br><br></div>How is that evaluated to True by foldl in elem'?<br><div><div><div><div><br></div><div>Thanks in advance for any explanation to this!<br></div></div></div></div></div>
<br></div></div>_______________________________________________<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>
<br></blockquote></div>
<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>
<br></blockquote></div><br></div>
_______________________________________________<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>