I write a simple hash tree structure in the following hash_lookup.hs
program:
module Main where data (Eq a) => HashTable ab = HashChildren (a, [HashTable ab]) | Hash (a, b) deriving (Show) getKey :: HashTable ab -> a getKey (HashChildren (k, hs)) = k getKey (Hash (k, h)) = k lookUp :: [a] -> HashTable ab -> Maybe (HashTable ab) lookUp [] table = return table lookUp _ (Hash _) = Nothing lookUp (p:path) (HashChildren (_, ts) ) = lookUp path ( head ( dropWhile (\x -> (getKey x) /= p) ts ) )
getKey is designed to retrieve the root key of the given HashTable, and lookUp accepts a list of strings and must follow the first path that it finds until it reaches the full path or works (I know this isnβt a Natural behavior for a tree, but this is what my textbook wants. "
I have two questions: 1) Why am I getting an error message telling me that a /= a
(from the last line) is not valid, as there is No instance for (Eq a)
(error message in the terminal), despite (Eq a)
in the data declaration?
2) Besides the error I get, and the seemingly strange behavior of the search function, is this a good or idiomatic Haskell?
thanks
source share