Haskell data structure fuzzy

I am trying to write a small file to try out a bag-like data structure. So far, my code is as follows:

data Fruit = Apple | Banana | Pear deriving (Eq, Show) data Bag a = EmptyBag | Contents [(a, Integer)] emptyBag :: Bag a emptyBag = EmptyBag unwrap :: [a] -> a unwrap [x] = x isObject theObject (obj, inte) = theObject == obj count :: Bag a -> a -> Integer count (Contents [xs]) theObject = snd (unwrap (filter (isObject theObject) [xs])) count EmptyBag _ = 0 

But when I try to run it, I get the error Failed to deduce (Eq a) from the context () arising from using "isObject" when ....

If I take the count function and call snd (spread (filter (isObject Banana) [(Apple, 1), (Banana, 2)])) it happily returns 2.

Any tips on why this is so, or tips on writing such a data structure would be greatly appreciated.

+4
source share
1 answer

(==) can only be used in a context that includes Eq , but when you declared count , you did not include this context. If I read correctly, it will be

 count :: Eq a => Bag a -> a -> Integer 

If you declare count without including a type, you can request ghci for the selected type; or just ask for the deduced type snd (unwrap (filter (isObject Banana) [(Apple,1),(Banana,2)]))

+6
source

All Articles