-2

I am learning Haskell and I just got a problem that seems to be quite hard.

I have a custom recursive data type for Trie.

data Trie keytype valuetype = TNode (Maybe valuetype) [Edge keytype valuetype]
            deriving (Show, Eq)

type Edge e a = (e, Trie e a)

And I am trying to implement functions that work with it. I found how to traverse it incorrect order.

isTrieValid :: Ord e => Trie e a -> [e]
isTrieValid (TNode _ []) = []
isTrieValid (TNode _ ((edge, TNode maybe innerlist):children)) =  edge : printChildren innerlist  ++ printChildren children
       where  
              printChildren :: Ord e => [Edge e valuetype] -> [e]
              printChildren [] = []
              printChildren ((edge_name, TNode _ innerlist):xs) =edge_name : printChildren innerlist ++ printChildren xs

Which for eg. for following trie returns all "keys" (edge names) -> "abbce"

trie1 = TNode (Just 42) [('a', TNode (Just 16) []),
         ('b', TNode Nothing
               [('b', TNode (Just 1) []),
                ('c', TNode (Just 5) []),
                ('e', TNode (Just 2) [])
               ])
        ]

Now I am trying to make functions that will find elements based on "key" like this:

trieLookup :: Ord e => [e] -> Trie e a -> Maybe a

But I am witnessing two problems. First is, if I traverse the Trie with recursion I am not sure how to handle the first element. Because there is no "key" for the first element what shall I return? I thought an empty string would work but I want the whole "Ord" type class to be used for keys, so that raises a type error.

And next function which I was trying to make will check the Trie validity.

isTrieValid :: Ord e => Trie e a -> Bool

What I understood is, that Trie must have this quality to be considered valid.

  • All elements following the current element are ordered by the first element (order by Ord e)
  • No two successors have the same key

So whole TRIE is valid if the root node is valid and all subtrees/subtriees (not sure how to write it) are also valid.

I wanted to make a function that would check validity for one node and then replicate it on all nodes using the function above, but I failed.

Thanks for any help.

  • **Mod note**: Stop flagging this question purely on the grounds that it's an active homework assignment. [We allow homework questions](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions) as long as they're [properly asked,](/help/ask) which this question appears to be (at least to me as a non-SME). It being a homework question isn't a close reason. We also [don't care if it's part of anything active](https://meta.stackoverflow.com/a/278808/6296561) or in violation of honor codes. – Zoe Nov 21 '21 at 00:21
  • That said, if this turns out to be closable for some other reason, go ahead and flag using the appropriate flags for the relevant reason, but it being an active homework assignment isn't such a reason. And Abdul, feel free to disregard these comments. You haven't done anything wrong here, and these comments aren't to address your behavior – Zoe Nov 21 '21 at 00:23
  • 2
    To be clear, "haven't done anything wrong" is a bit over-broad - we can't say for sure. Abdul has done nothing against Stack Overflow rules or policies, but may still have done something wrong according to school policy. As mentioned, Stack Overflow cannot enforce those, so indeed the question is on-topic here, but it may still be "wrong" in a broader sense. – amalloy Nov 21 '21 at 20:28

1 Answers1

1

I suggest changing your Trie type. All the properties of isTreeValid are easily maintained by using Data.Map k v instead of [Edge k v].

And I don't really understand what problem you're talking about with trieLookup. It seems quite simple that if the input key ([e]) is empty, you return the value at the root, and otherwise you descend further into the tree. At what point does the question of what how to treat the root arise? If you need help with that function, include your attempt to solve it and describe what's wrong or where it needs work.

amalloy
  • 89,153
  • 8
  • 140
  • 205
  • I would love to change it but I cant. This was the assignment. (I am at highschool) Problem with trieLookup is that to do this effectively, I have to check the keys so i do not go to branches where cannot be the value I am looking for. So for the TRIE i have above key "bb" shall return 1. So i need to compare b with a and b and find that a is *something* and ignore this whole branch. I come from python so haskell is way different... sorry if the problems seems trivial. I just do not see it. I can compare strings, but when i try it in console it does not do what i need it to do. – Abdul Hajabi Nov 17 '21 at 09:49