如何概括非平凡的记录更新?这是一个简单的示例,它显式地将项目引用更新为其新所有者。
data Owner = Owner {
items :: [Item]
} deriving (Show)
data Item = Item {
name :: String,
owner :: Owner
}
instance Show Item where
show (Item name _) = name
item1 = Item "a" owner1
item2 = Item "b" owner1
item3 = Item "c" owner1
owner1 = Owner [item1,item2,item3,Item "d" owner1]
owner2 = Owner $ map (\(Item n o) -> Item n owner2) $ items owner1