<div dir="ltr"><font face="monospace">data Tree a = Leaf a | Node (Tree a) (Tree a)<br></font><div><br></div><div>How could I modify this definition so that a Node could be:</div><div><br></div><div><font face="monospace">Node (Tree a) Null/Nothing/[] -- basically a zilch that would match anything or nothing, because it would make programming simple. Alternatively:</font></div><div><font face="monospace"><br></font></div><div><font face="monospace">Node (Tree a)</font></div><div><font face="monospace"><br></font></div><div><font face="monospace">-- Would I be looking at,</font></div><div><font face="monospace"><br></font></div><div><font face="monospace">data Tree a = Leaf a | Node (Tree a) Maybe (Tree a)<br><br class="inbox-inbox-Apple-interchange-newline"></font></div><div><font face="monospace">-- Or possibly the best I can do is,</font></div><div><font face="monospace"><br></font></div><div><font face="monospace">data Tree a = Leaf a | Node (Tree a) (Tree a) | Node (Tree a)<br><br class="inbox-inbox-Apple-interchange-newline"></font></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr">On Mon, Sep 3, 2018 at 3:06 AM Rishi Rajasekaran <<a href="mailto:rajasekaran.rishi@gmail.com">rajasekaran.rishi@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="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></div></div><div dir="auto"><div dir="auto"><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" target="_blank">trent.shipley@gmail.com</a>> wrote:<br></div></div></div></div><div dir="auto"><div dir="auto"><div class="gmail_quote" dir="auto"><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></blockquote></div></div></div><div dir="auto"><div dir="auto"><div class="gmail_quote" dir="auto"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" rel="noreferrer" target="_blank">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>
_______________________________________________<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>