Sop*_*oud 8 haskell declaration data-structures
我有一个函数返回产品的价格,它目前看起来像
priceOfProduct:: Int -> Int -> Int
是否值得宣布
type Price = Int
这样功能就变成了
priceOfProduct :: Int -> Int -> Price ?
我想这样做,然后继续使用Ints元组,如果它们是他们自己的数据结构,它们可能看起来更好.
priceVsTaxed -> Price -> Int -> (Price, Price)
这有用吗?这有必要吗?
这是一个很好的Haskell风格吗?
声明一个看起来更像是重命名现有数据结构好的样式的数据结构?
lef*_*out 11
并不总是值得定义额外的类型,但绝对避免Int -> ... -> Int签名.这使得很难理解如何使用函数.
所以实际上我会说你不仅应该重命名结果,还要特别重命名参数.然后,如果有人想要使用你的函数,他们可以让编译器解释参数:
foo :: Price
foo = priceOfProduct _ _ + priceOfProduct _ _
会给出一个编译器(GHC> = 7.10)的消息
Foo.hs:Y:X: error:
    • Found hole: _ :: PriceOfProductFirstArgument
    • In the first argument of ‘priceOfProduct’, namely ‘_’
      In the expression: priceOfProduct _ _
      In an equation for ‘foo’: main = foo = priceOfProduct _ _ + priceOfProduct _ _
但是你应该考虑一下你是否不想让类型区分变得更加严格:一个简单的typedef永远不会让你把参数置于错误的顺序,所以也许你最好把它做成
newtype Price = Price {priceInEuroCents :: Int}
这也避免了价格给出的货币/数量的模糊性.
| 归档时间: | 
 | 
| 查看次数: | 105 次 | 
| 最近记录: |