[Haskell-cafe] how to make this work recursive ?

Sumit Sahrawat, Maths & Computing, IIT (BHU) sumit.sahrawat.apm13 at iitbhu.ac.in
Sat Feb 28 18:49:45 UTC 2015


Don't worry. Keep these points in mind and re-read my previous mail.

Regarding data vs. type constructors.

1) Leaf :: MessageTree (Leaf is a MessageTree)
2) MessageTree is a type constructor (takes types and gives new types)
3) Node is a data constructor (takes values and gives a new MessageTree)

Regarding insertion.

1) The "insert" function takes a message and tree, and returns a tree with
that message inserted into it
2) We absolutely have to provide it a tree
3) If we insert an invalid message in the tree, it does not get added. Then
we get ..... the same tree back.

If you still cannot see a problem in your code, ask yourself the following:

1) What type does the output of insert (MessageTree Leaf) have, and what
type should it have
2) What happens if we insert an invalid string in a non-empty tree
(according to your code)

Don't forget to re-read the previous mail.
Hope this drives the point home.

On 28 February 2015 at 22:59, Roelof Wobben <r.wobben at home.nl> wrote:

>  Sorry,
>
> But I do not understand what you mean.
>
> What I try to do is this
>
> If you have a LogMessage which contains " This is not the right format "
> then the LogMessage is not inserted in the tree.
> Every other message are inserted in the tree.
>
> So that is why I make a MessageTree with only one Leaf.
>
> Roelof
>
>
>
> Sumit Sahrawat, Maths & Computing, IIT (BHU) schreef op 28-2-2015 om 16:08:
>
> You're missing an argument to insert, assuming that it is indeed there,
> consider this:
>
>      insert msg tree = tree'
>       where tree' = undefined
>     -- Take a LogMessage (msg) and a MessageTree (tree)
>     -- and return a new MessageTree (tree')
>
>  Therefore, the result of insert in your case, i.e. "MessageTree Leaf"
> should be of type MessageTree.
> And thus, we get
>
>      MessageTree Leaf :: MessageTree
>     -- Takes a (Leaf :: LogMessage) and produces a MessageTree
>
>  which implies,
>
>      MessageTree :: LogMessage -> MessageTree
>     -- A Data constructor
>
>  Obviously no such data constructor exists. A data constructor of this
> type would exist only if you wrote something like:
>
>      data MessageTree = MessageTree LogMessage
>                      | ...
>
>  which would not make sense.
>
> On 28 February 2015 at 19:09, Roelof Wobben <r.wobben at home.nl> wrote:
>
>>  I tried this :
>>
>> insert :: LogMessage -> MessageTree -> MessageTree
>>  insert s =
>>    case words s of
>>        (_:_: "This is not the right format") -> MessageTree Leaf
>>        _                                     -> MessageTree Node
>> LogMessage Leaf
>>
>>
>> But I see this errormessages which I do not understand :
>>
>>
>>  src/LogAnalysis.hs at 38:49-38:60
>> Not in scope: data constructor
>> MessageTree
>>   src/LogAnalysis.hs at 39:49-39:60
>> Not in scope: data constructor
>> MessageTree
>>
>>
>>
>> Mike Meyer schreef op 28-2-2015 om 12:17:
>>
>>  On Sat, Feb 28, 2015 at 5:07 AM, 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
>>>
>>
>>  Whether you need recursion or not depends on where you're to insert the
>> message. For instance, you could turn them into a list with:
>>
>>  insert message tree = Node tree message Leaf
>>
>>  Try drawing a few pictures of what that produces, and you'll see why
>> it's probably not what you want.
>>
>>  You should be able to tell by looking at the MessageTree type that you
>> have two cases: one where you're passed something matching a Node, and one
>> where you're passed something that's just a Leaf. The latter can't be
>> recursive, so it's going to terminate any recursion. Following the advice
>> given to you for the Hanoi problem, try writing it first.
>>
>>  Then all you have to do is figure out how to handle the case where
>> you're given a Node instead of a Leaf.  Figuring out where in the
>> MessageTree you want to insert the Logmessage will probably be required to
>> do that.
>>
>>
>>
>> _______________________________________________
>> 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/20150301/abb442d1/attachment-0001.html>


More information about the Haskell-Cafe mailing list