[Haskell-cafe] how to make this work recursive ?
Sumit Sahrawat, Maths & Computing, IIT (BHU)
sumit.sahrawat.apm13 at iitbhu.ac.in
Sat Feb 28 11:16:33 UTC 2015
First, consider the two possible cases,
insert :: LogMessage -> MessageTree -> MessageTree
insert m Leaf = Node Leaf m Leaf -- Inserting into an empty
tree (base case for recursion)
insert m (Node l m r) = undefined -- Try working it out here.
You will need to decide where to insert the message in a way such that the
properties of binary tree are not violated.
If you can decided that at each recursive step and proceed accordingly, you
will hit the base case soon enough.
On 28 February 2015 at 16:37, Roelof Wobben <r.wobben at home.nl> wrote:
>
> That part I understand .
>
> it's more how I do it here :
>
> Lets say I have this :
>
> data MessageType = Info
> | Warning
> | Error Int
> deriving (Show, Eq)
>
> type TimeStamp = Int
>
> data LogMessage = LogMessage MessageType TimeStamp String
> | Unknown String
> deriving (Show, Eq)
>
> data MessageTree = Leaf
> | Node MessageTree LogMessage MessageTree
> deriving (Show, Eq)
>
> Now I have to put all the LogMessages in a binary tree.
> with this signature : insert :: LogMessage -> MessageTree -> MessageTree
>
> I think I need recursion here but I still not see how I can make the
> clauses.
> Normally you can say somethig like this [] -> 0 or 1 -> but here it
> seems to be all seperate logMessages
>
> Roelof
>
>
>
>
>
> Sumit Sahrawat, Maths & Computing, IIT (BHU) schreef op 28-2-2015 om 11:53:
>
> Draw the trees, and then try to partition every node into (left, element,
> right).
>
> For example, in your first example (Node leaf "msg 1" leaf), we get
>
> Node "msg 1"
> ├── leaf
> └── leaf
>
> In the second example,
>
> Node "msg 1"
> ├── leaf
> └── Node "msg 2" -- A sub-tree
> ├── leaf
> └── leaf
>
> Now, substituting a leaf with a Node in the above tree will lead to
> larger trees.
> For another example, look here:
> http://en.wikipedia.org/wiki/Tagged_union#Examples
>
> On 28 February 2015 at 14:10, Roelof Wobben <r.wobben at home.nl> wrote:
>
>> Hello,
>>
>> I found out that for 1 item I can do this : node = Node leaf "msg 1" leaf
>> And for 2 items I can do this : node = Node leaf "msg1" (Node leaf
>> "msg2" leaf)
>>
>> 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
>>
>> Roelof
>>
>>
>> _______________________________________________
>> Haskell-Cafe mailing list
>> Haskell-Cafe at haskell.org
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>>
>
>
>
> --
> Regards
>
> Sumit Sahrawat
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>
>
--
Regards
Sumit Sahrawat
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20150228/056fe79b/attachment.html>
More information about the Haskell-Cafe
mailing list