sholizar

Untitled

May 14th, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- i have those data types.
  2. -- TimeStamp is an Int
  3. -- MessageType is not important for this question :-).
  4.  
  5. data LogMessage = LogMessage MessageType TimeStamp String
  6.                 | Unknown String
  7.   deriving (Show, Eq)
  8.  
  9. data MessageTree = Leaf
  10.                  | Node MessageTree LogMessage MessageTree
  11.   deriving (Show, Eq)
  12.  
  13. {-
  14. the exercise is this:
  15. A MessageTree should be sorted by timestamp: that is, the timestamp
  16. of a LogMessage in any Node should be greater than all timestamps
  17. of any LogMessage in the left subtree, and less than all timestamps
  18. of any LogMessage in the right child.
  19.  
  20. Unknown messages should not be stored in a MessageTree since
  21. they lack a timestamp.
  22.  
  23. i need to create this function:    -}
  24. insert :: LogMessage -> MessageTree -> MessageTree
  25.  
  26. {-
  27. which inserts a new LogMessage into an existing MessageTree, producing
  28. a new MessageTree. insert may assume that it is given a
  29. sorted MessageTree, and must produce a new sorted MessageTree
  30. containing the new LogMessage in addition to the contents of the
  31. original MessageTree.
  32. However, note that if insert is given a LogMessage which is
  33. Unknown, it should return the MessageTree unchanged.
  34.  
  35. can you please give me a hint?
  36. what i have done so far is this:
  37. -}
  38.  
  39. insert :: LogMessage -> MessageTree -> MessageTree
  40. insert (Unknown _) m    = m
  41. insert l           Leaf = Node Leaf l Leaf
  42.  
  43. {-
  44. but i can get a `insert` with a non empty MessageTree and i don't understand how to insert a new LogMessage to a non empty MessageTree.
  45. please do not post answers.
  46. just a hint how to proceed.
  47. thanks!
  48. -}
Advertisement
Add Comment
Please, Sign In to add comment