My student assignment I'm doing for Haskell programming includes a task I'm a little bit puzzled to solve. The things are given so: an instance of Functor class to be created just for a set-based new type. There is a declaration of that:
newtype Set x = Set { contains :: (x -> Bool) }
It's a case for me to understand what means if fmap
serves to be applied to something like a set of predicates. When doing about previous tasks, I've already defined fmap
rather with functions like (+3)
(to alter integers), (toUpper)
(to strings) etc. It's first time I'm dealing with anything beyond normal types (various numerics, String, Char). There is my humble attempt to start:
instance Functor Set where
fmap f (Set x) = if (contains x) == True then Set (f x) else Set x
Surely, it's a nonsense code, but I suppose some True/False need to be evaluated before fmap
apllication goes well. But, first of all, would you please explain the matter of set of predicates to elaborate more sensible approach?