Questions tagged «typeclass»

1
使用Haskell类型类强制交换
我想为可以交叉在一起的几何对象定义一个类型类: class Intersect a b c | a b -> c where intersect :: a -> b -> c -- Language extensions: -XMultiParamTypeClasses, -XFunctionalDependencies 这个想法是要有一个通用的交集函数,可以处理不同类型的对象。可以想象这样的情况 instance Intersect Line Plane (Maybe Point) where ... instance Intersect Plane Plane (Maybe Line) where ... 但我也想声明交集是可交换的: instance (Intersect a b c) => Intersect b …

2
为什么在Haskell中不绑定Enum的子类
似乎任何Bounded实例都应具有健全的Enum实现。我个人无法想到一个反例,尽管如果有人提出了一个非病态的例子,那么我会理解为什么不是这样。 从这:i两个类型类上看,似乎标准库中当前唯一的例外是元组,它们是有界的,但不是枚举。但是,任何绑定元组也必须以合理的方式可枚举,方法是简单地增加最后一个元素,然后在到达maxBound时环绕它。 此更改可能还涉及在Bounded中添加predB和/ nextB或类似的东西,以安全/循环的方式遍历Enum值。在这种情况下toEnum 0 :: (...)等于(toEnum 0, toEnum 0, ...) :: (...)
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.