<div dir="auto"><div dir="auto">Hi Trent </div><div dir="auto"><br></div><div>For executing your approach, you can do the following:</div><div dir="auto">1. Compute the number of leaves in the left subtree first. </div><div dir="auto">2. Pass that computed value into leaves' call for the right subtree<br><br></div><div dir="auto">Regards</div><div dir="auto"><br></div><div dir="auto">Rishi</div><div dir="auto"><br></div><div dir="auto"><br><div class="gmail_quote" dir="auto"><div dir="ltr">On Mon, 3 Sep, 2018, 2:36 PM trent shipley, <<a href="mailto:trent.shipley@gmail.com">trent.shipley@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">Given<div>data Tree a = Leaf a | Node (Tree a) (Tree a)<br></div><div><br></div><div>Write a leaf counter.</div><div><br></div><div>Hutton suggests:</div><div><br></div><div><div><font face="monospace">leaves :: Tree a -> Int</font></div><div><font face="monospace">leaves (Leaf _) = 1</font></div><div><font face="monospace">leaves (Node l r) = leaves l + leaves r</font></div></div><div><font face="monospace"><br></font></div><div>I tried:</div><div><br></div><div><div><font face="monospace">leavesTrent :: Tree a -> Int</font></div><div><font face="monospace">leavesTrent = leaves' 0</font></div><div><font face="monospace">         where</font></div><div><font face="monospace">           leaves' n (Leaf a) = n + 1</font></div><div><font face="monospace">           leaves' n (Node l r) = (leaves' n l), (leaves' n r)</font></div></div><div><font face="monospace"><br></font></div><div>The idea is:</div><div><br></div><div>If it is a leaf, add one to the accumulator.  (Following Hutton's explanation of how sum works if defined with foldl.)   If it is a tree, proceed down the left subtree recursively, until you get to a leaf, then roll up to the right subtree.  The problem (among the problems) is that I don't know how to tell the compiler to do all lefts, before backing up to go right.  I only know how to do that using a real operator like "+" or foo (l, r).</div><div><br></div><div>Is that kind of no-op recursive branching possible?</div><div><br></div><div>Trent.</div></div>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank" rel="noreferrer">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
</blockquote></div></div></div>