<div dir="ltr">First, consider the two possible cases,<div><br></div><div><font face="monospace, monospace">insert :: LogMessage -> MessageTree -> MessageTree</font></div><div><span style="font-family:monospace,monospace">insert</span><span style="font-family:monospace,monospace"> </span><span style="font-family:monospace,monospace">m</span><span style="font-family:monospace,monospace"> Leaf = Node Leaf m Leaf -- Inserting into an empty tree (base case for recursion)</span></div><div><font face="monospace, monospace">insert m (Node l m r) = undefined -- Try working it out here.</font></div><div><br></div><div>You will need to decide where to insert the message in a way such that the properties of binary tree are not violated.</div><div>If you can decided that at each recursive step and proceed accordingly, you will hit the base case soon enough.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On 28 February 2015 at 16:37, Roelof Wobben <span dir="ltr"><<a href="mailto:r.wobben@home.nl" target="_blank">r.wobben@home.nl</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000">
<div><br>
That part I understand .<br>
<br>
it's more how I do it here :<br>
<br>
Lets say I have this : <br>
<br>
data MessageType = Info<br>
| Warning<br>
| Error Int<br>
deriving (Show, Eq)<br>
<br>
type TimeStamp = Int<br>
<br>
data LogMessage = LogMessage MessageType TimeStamp String<br>
| Unknown String<br>
deriving (Show, Eq)<br>
<br>
data MessageTree = Leaf<br>
| Node MessageTree LogMessage MessageTree<br>
deriving (Show, Eq)<br>
<br>
Now I have to put all the LogMessages in a binary tree.<br>
with this signature : insert :: LogMessage -> MessageTree ->
MessageTree<br>
<br>
I think I need recursion here but I still not see how I can make
the clauses. <br>
Normally you can say somethig like this [] -> 0 or 1 ->
but here it seems to be all seperate logMessages <br>
<br>
Roelof<br>
<br>
<br>
<br>
<br>
<br>
Sumit Sahrawat, Maths & Computing, IIT (BHU) schreef op
28-2-2015 om 11:53:<br>
</div><div><div class="h5">
<blockquote type="cite">
<div dir="ltr">Draw the trees, and then try to partition every
node into (left, element, right).
<div><br>
</div>
<div>For example, in your first example (Node leaf "msg 1"
leaf), we get</div>
<div><br>
</div>
<div><font face="monospace, monospace">
<div>Node "msg 1"</div>
<div>├── leaf</div>
</font>
<div style="font-family:monospace,monospace">└── leaf</div>
<div style="font-family:monospace,monospace"><br>
</div>
In the second example,</div>
<div><br>
<font face="monospace, monospace">
<div>Node "msg 1"</div>
<div>├── leaf</div>
</font><font face="monospace, monospace">
<div>└── Node "msg 2" -- A sub-tree</div>
</font><font face="monospace, monospace">
<div> ├── leaf</div>
<div> └── leaf</div>
<div><br>
</div>
</font>Now, substituting a <font face="monospace, monospace">leaf</font> with
a <font face="monospace, monospace">Node</font> in the above
tree will lead to larger trees.</div>
<div>For another example, look here: <a href="http://en.wikipedia.org/wiki/Tagged_union#Examples" target="_blank">http://en.wikipedia.org/wiki/Tagged_union#Examples</a></div>
</div>
<div class="gmail_extra"><br>
<div class="gmail_quote">On 28 February 2015 at 14:10, Roelof
Wobben <span dir="ltr"><<a href="mailto:r.wobben@home.nl" target="_blank">r.wobben@home.nl</a>></span>
wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello,<br>
<br>
I found out that for 1 item I can do this : node = Node leaf
"msg 1" leaf<br>
And for 2 items I can do this : node = Node leaf "msg1"
(Node leaf "msg2" leaf)<br>
<br>
But now I wonder how I can make this work with recursion. It
seems that I keep the first 2 items and change the 3th one
from leaf to another node<br>
<br>
Roelof<br>
<br>
<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br>
</blockquote>
</div>
<br>
<br clear="all">
<div><br>
</div>
-- <br>
<div>
<div dir="ltr">
<div>
<div dir="ltr">
<div dir="ltr">
<div>Regards</div>
<div dir="ltr">
<div><br>
</div>
<div>Sumit Sahrawat</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
<br>
</div></div></div>
<br>_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div>Regards</div><div dir="ltr"><div><br></div><div>Sumit Sahrawat</div></div></div></div></div></div></div>
</div>