小编eph*_*ion的帖子

Haskell自定义数据类型,实例Num和冗余

我正在Haskell开发一小部分举重工具作为学习练习.我已经定义了一种数据类型Weight:

data Weight = Wt Float Unit 
              deriving (Show, Eq)

data Unit   = Lb | Kg 
              deriving (Show, Eq)

instance Num Weight where
  Wt x Lb + Wt y Lb = Wt (x + y) Lb
  Wt x Lb * Wt y Lb = Wt (x * y) Lb
  negate (Wt x Lb)  = Wt (negate x) Lb
  abs    (Wt x Lb)  = Wt (abs x) Lb
  signum (Wt x Lb)  = Wt (signum x) Lb
  fromInteger x …
Run Code Online (Sandbox Code Playgroud)

haskell types instance

2
推荐指数
1
解决办法
604
查看次数

标签 统计

haskell ×1

instance ×1

types ×1