ADT的集合,没有重复的构造函数

时间:2014-07-13 07:48:52

标签: haskell map set

如果我有数据类型:

data Component = PositionC Double Double | HealthC Double | NameC String
  • 我想要一个Component s
  • 的集合
  • 我不会存储任何重复项(即:它不会有两个HealthC)
  • 我不想手动复制代码

我可以使用哪种技术,数据结构,TemplateHaskell功能来满足上述标准?

我已经尝试过Data.Set和Data.Map但没有成功。

Data.Map尝试:

import Data.Map

data Component = PositionC Double Double | HealthC Double | NameC String

-- Unacceptable code duplication
data ComponentType = PositionCT | HealthCT | NameCT
    deriving (Eq, Ord)

type ComponentMap = Map ComponentType Component

foo :: ComponentMap
-- More annoying code duplication
foo = fromList [(HealthCT, HealthC 100), (NameCT, NameC "John")] 

fooHealth = foo ! HealthCT -- However, accessing works great

Data.Set尝试:

import Data.Set (Set)
import qualified Data.Set as Set

data Component = PositionC Double Double | HealthC Double | NameC String
    deriving Eq

foo :: Set Component
foo = Set.fromAscList [HealthC 100, NameC "John"] -- This works well

-- But accessing elements is very bad
fooHealth = Set.elemAt 0 $ Set.filter p foo
    where p (HealthC _) = True
          p _           = False

1 个答案:

答案 0 :(得分:6)

一个选项是使用带有可选字段的记录:

data Component = Component 
                 { positionC :: Maybe (Double, Double)
                 ; healthC :: Maybe Double 
                 ; nameC :: Maybe String
                 }

直观地说,访问字段也比使用SetMap便宜。

设置字段

moveToOrigin c = c { positionC = Just (0,0) }

获得一个字段

-- Requires health to be present
isDead c = fromJust (healthC c) == 0

-- If health is unknown returns False
saferIsDead c = health C == Just 0