Reputation: 365
I'm a new of Haskell. I've understand what is functor and category.
But I have another question. Must I use a functor with Functor
type class?
I think I can also define another type class to do the same as Functor
?
Both of them can do the same.
instance Functor MyData where
fmap f (MyData a) = MyData (f a)
And
class MyDataFunctor f where
fmap :: (a -> b) -> f a -> f b
instance MyDataFunctor MyData where
fmap f (MyData a) = MyData (f a)
Upvotes: 0
Views: 148
Reputation: 120711
Sure you can do this. It's just kinda reinventing the wheel. The advantage of standard classes is that everybody will make their types instances of them if appropriate, so if you write a function that can operate on a generic Functor f
then it'll work with a huge number of different type constructors f
from hundreds of libraries. If you change the function to require MyDataFunctor f
instead, it'll do exactly the same thing, but only with your own type MyData
.
The only sensible reason you might want to write your own class is if you want different behaviour.
Upvotes: 8