Haskell中的参数化数据结构 [英] parameterized data structures in haskell
问题描述
你能否就如何在haskell中表示参数化数据结构提供建议?例如在代表服装店内容的应用程序中,我可能会有男装和女装衣架的实例。这些参数可以是分层的,例如,基于年龄组。所以我可以有一个架子(男士(14岁以下))或者一个架子(女士(成人))。而且,一些限制可能适用于机架参数,例如,没有婴儿服装。和java类一样,我需要一些可以对这些实例进行操作的成员函数。
类似这样的事情:
data Rack = Rack {allClothesOnRack :: [Clothing],
withClothing :: Clothing - >也许机架,
withoutClothing ::服装 - >也许Rack}
emptyRack ::(Clothing - > Bool) - > Rack
emptyRack isAcceptable = rack isAcceptable []
rack ::(Clothing - > Bool) - > [服饰] - > Rack
rack isAcceptable = updated
其中更新的商品= Rack {allClothesOnRack =商品,
withClothing =添加,
withoutClothing =删除}
添加商品| isAcceptable item = Just $ updated(item:items)
|否则=无
删除项目 - 留作练习
创建一个 Rack
,其中具有一个函数,用于指定是否可以将特定的衣服放在衣架上。这个函数可以使用服装
(在我的例子中没有定义)它喜欢的任何属性。
例如。如果 isBabyClothing :: Clothing - > Bool
返回 True
,当您给出一件婴儿服装时,您创建一个空的婴儿衣服架, emptyRack isBabyClothing
。
使用 allClothesOnRack
函数获取机架上所有衣服的列表。
例如 allClothesOnRack(emptyRack isBabyClothing)
总会产生 []
。
使用 withClothing
和 withoutClothing
函数在衣架上添加/删除一件衣服。如果这是不可能的,它们每个都会返回 Nothing
>(衣服可能不放在该货架上,或者它不存在于货架上,因此无法移除) ,或只需
更新的货架。
Can you advise on how to represent parameterized data structures in haskell? e.g. in an application which represents contents of a garments shop, I might have instances of racks for men's and women's clothes. These parameters can be hierarchical, e.g. based on age groups. So I can have a rack(men(under14)), or a rack(women(adult)). Also, some constraints may apply to the rack parameters, e.g. no baby clothes. And as with java classes, I would need some member functions which would operate on these instances.
How about something like this:
data Rack = Rack {allClothesOnRack :: [Clothing],
withClothing :: Clothing -> Maybe Rack,
withoutClothing :: Clothing -> Maybe Rack}
emptyRack :: (Clothing -> Bool) -> Rack
emptyRack isAcceptable = rack isAcceptable []
rack :: (Clothing -> Bool) -> [Clothing] -> Rack
rack isAcceptable = updated
where updated items = Rack {allClothesOnRack = items,
withClothing = adding,
withoutClothing = removing}
adding item | isAcceptable item = Just $ updated (item : items)
| otherwise = Nothing
removing item -- left as an exercise
You create a Rack
with a function that specifies whether a particular item of clothing may be put on the rack. This function can use any property of Clothing
(not defined in my example) it likes.
e.g. if isBabyClothing :: Clothing -> Bool
returns True
when given an item of baby clothing, then you create an empty baby clothes rack with emptyRack isBabyClothing
.
Use the allClothesOnRack
function to get a list of all the clothes on the rack.
e.g. allClothesOnRack (emptyRack isBabyClothing)
will always yield []
.
Use the withClothing
and withoutClothing
functions to add/remove an item of clothing to/from the rack. They each return Nothing
if this is not possible (either the item of clothing may not be put on that rack, or it is not present on the rack and so cannot be removed), or Just
the updated rack otherwise.
这篇关于Haskell中的参数化数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!