4
有了这个:类型类,重载和实例声明
data Rectangle = Rectangle Height Width
data Circle = Circle Radius
class Shape a where
area :: a -> Float
perimeter :: a -> Float
instance Shape Rectangle where
area (Rectangle h w) = h * w
perimeter (Rectangle h w) = 2*h+w*2
instance Shape Circle where
area (Circle r) = pi * r**2
perimeter (Circle r) = 2*pi*r
volumenPrism base height = (area base) * height
surfacePrism shape h = (area shape) * 2 + perimeter shape * h
为什么不能我写这篇文章? a
是一种类型,为什么它不工作?
instance (Shape a) => Eq a where
x==y = area x == area y
显然这样做是这样的:
instance Eq Circle where
x==y = area x == area y
第一的圆,然后矩形works..but似乎不正确的做法。
这是什么我没有得到所有这一切?
Ty
它不是,它不会回溯 - 那就是它并不意味着你认为它。 '实例Shape a => Eq a'意味着“所有类型都是Eq的实例,使用这个实例会导致为所使用的类型添加一个”形状“约束。 – Carl
顺便说一句,这是它唯一可以表达的意思,因为这个开放世界的假设。无论你多么小心,总会在后面添加一个实例,使事情变得模糊。 – Carl
在操作上,分辨率不会发生变化。从语义上讲,实例按照您的描述进行定义。不确定哪个是因果关系:-)开放世界的假设已经被例如'IncoherentInstances',所以我不完全遵循这一点。 –