Nod*_*.JS 2 syntax haskell typeclass
我是 Haskell 的新手,我正在尝试理解语法。我有数据类型Vec,它实现了Show,Floating和Foldable. 但是语法不同Foldable,为什么?
data Vec a = Vec [a]
instance Show a => Show (Vec a) where
show (Vec x) = show x
instance Floating a => Floating (Vec a) where
pi = Vec (repeat pi)
exp (Vec x) = Vec (map exp x)
log (Vec x) = Vec (map log x)
sin (Vec x) = Vec (map sin x)
cos (Vec x) = Vec (map cos x)
instance Foldable Vec where
foldr f x (Vec y) = foldr f x y
Run Code Online (Sandbox Code Playgroud)
您的实例Foldable是“自包含”的,它不依赖于其他实例(此或不同类型的类)的可用性。
但是对于您的实例,Show (Vec a)您还需要一个Show afirst实例(以便您可以在您的实现中调用show x)。
该=>语法建立了这种要求/依赖性。