【问题标题】:Is it possible to use guards in function definition in idris?是否可以在 idris 的函数定义中使用守卫?
【发布时间】:2014-09-18 19:31:41
【问题描述】:

在 haskell 中,可以这样写:

containsTen::Num a => Eq a => [a] -> Bool
containsTen (x : y : xs)
    | x + y == 10 = True
    | otherwise = False

是否可以在 Idris 中编写等价的东西,而无需使用 ifThenElse(我的实际情况比上述情况更复杂)?

【问题讨论】:

    标签: syntax pattern-matching idris guard-clause


    【解决方案1】:

    Idris 没有与 haskell 完全相同的模式保护。 with 子句在语法上相似(但更强大,因为它支持存在依赖类型的匹配):

    containsTen : Num a => List a -> Bool
    containsTen (x :: y :: xs) with (x + y)
        | 10 = True
        | _  = False
    

    您可以查看Idris tutorial 部分7 Views 和“with”规则

    【讨论】:

    • 这与 Haskell 示例中的守卫不同,后者允许| x+y == 10 ... | func (x * y + 52) > 42 = ... 之类的东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-12
    • 1970-01-01
    • 2012-10-30
    • 1970-01-01
    • 2019-02-22
    • 2016-04-06
    相关资源
    最近更新 更多