Fer*_*rry 2 implementation haskell instance
如果有两种不同的数据类型,但它们具有类似的功能:
type model = String
type priceOfC = Int
data Car = Cars model priceOfC
ComparePricesCar :: Car -> Car -> Int
.... (some codes here to compare prices between two cars)
type color = String
type priceOfB = Int
data Bike = Bikes color priceOfB
ComparePricesBike :: Bike -> Bike -> Int
.... (some codes here to compare prices between two bikes)
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是实现类型类"流量",让Car和Bike成为Traffic的实例.并且所有实例都将实现一个名为"comparePrice"的函数.
我试过了:
class Traffic a where
comparePrice :: a -> a -> Int
instance Traffic Car where
comparePrice (Cars a b)(Cars c d) = ComparePricesCar (Cars a b)(Cars c d)
instance Traffic Bike where
comparePrice (Bikes a b)(Bikes c d) = ComparePricesBike (Bikes a b)(Bikes c d)
Run Code Online (Sandbox Code Playgroud)
我似乎没有使用错误消息,我多次声明comparePrice.如何使代码作为问题描述?任何帮助,thx!
归档时间: |
|
查看次数: |
113 次 |
最近记录: |