I have some difficulties to understand when use and when not use typeclass in my code. I mean create my own, and not use already defined typeclasses, of course. By example (very stupid example), should I do:
data Cars = Brakes | Wheels | Engine
data Computers = Processor | RAM | HardDrive
class Repairable a where
is_reparaible :: a -> Bool
instance Repairable Cars where
is_repairable (Brakes) = True
is_repairable (Wheels) = False
is_repairable (Engine) = False
instance Repairable Computers where
is_repairable (Processor) = False
is_repairable (RAM) = False
is_repairable (HardDrive) = True
checkState :: (Reparaible a) => a -> ...
checkState a = ...
(Obviously, this is an stupid, incomplete example).
But this is a lot for a little use, no? Why I shouldn't do something simple and only defining functions without defining new data types and typeclasses (with their instances).
This example is too simple, but in facts I often see somethings like that (new data types+typeclasses+instances) when I browse Haskell code on github instead of only defining functions.
So, when I should create new data types, typeclasses etc and when should I use functions?
Thanks.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…