haskell - 在 Haskell 中是否可以先编写包罗万象的模式?还是使用“负面”模式?
问题描述
有时,如果某个模式规则需要一些特殊的 rhs,通过 rhs 使其更具可读性where
,我最终会得到这样的结果
data D = A | B | C
func :: D -> b
func A = special_case
where
special_case = other helper
other = aaaa
helper = bbb
func _ = bla
由于冗长的where
. 如果我能写出这样的东西就好了:
func :: D -> b
func !A = bla -- imaginary syntax
func A = special_case
where
special_case = other helper
other = aaaa
helper = bbb
我认为它仍然不会被称为包罗万象,而是“包罗万象”,但是有什么办法可以做到这一点吗?
解决方案
如果你不需要绑定任何东西,你可以这样做:
isA :: D -> Bool
isA A = True
isA _ = False
func :: D -> b
func d | not (isA d) = bla
func A = special_case where ...
(您可以交替实现isn'tA
以避免not
. 但是,虽然我isA
经常看到类似定义的函数,但我不相信我曾经isn'tA
在野外看到过类似的。)
对于编译器来说,这个匹配是否完整可能并不明显。你可以像这样解决这个问题:
type A'sFields = () -- your specific D doesn't have any fields for A,
-- but this pattern is general enough to use for
-- constructors that do
fromA :: D -> Maybe A'sFields
fromA A = Just ()
fromA _ = Nothing
func :: D -> b
func d = case fromA d of
Nothing -> bla
Just () -> special_case where ...
推荐阅读
- android - 使用实时数据库和 recyclerview 进行大小写敏感搜索
- r - 如何在ggplot中的月度/年度数据上显示十年趋势
- cookies - 为什么将刷新令牌存储在仅 HTTP 的 Cookie 中以防止 CSRF 攻击?
- php - 使用 jAutoCalc 时动态表单插入 SQL php
- javascript - 如何使 onclick 函数与 jquery 一起工作?
- types - 将值从highestbars() 传递到lowestbars() 的问题
- c# - 在 OpenXML 中创建包含变量列的表
- python - 如何使用 pandas 合并多个数据框并为假人添加一列?
- python - 通过在列标题中查找唯一值来更新 csv 行中的“单元格”?
- amazon-web-services - 无法在 AWS 中 ping 域名