【问题标题】:Why does guard need the Ord typeclass in Idris?为什么守卫需要 Idris 中的 Ord 类型类?
【发布时间】:2018-05-10 22:44:15
【问题描述】:

在另一个问题 (How to write a simple list-based quicksort in Idris?) 中,我试图理解为什么 Prelude.Applicative.guard 需要 Ord 类型类。

Guard 定义为like this:

guard : Alternative f => Bool -> f ()
guard a = if a then pure () else empty

查看Alternative 接口docs(我还没有真正理解它在代码中是如何定义的,但我在学习 Idris 方面还不是很远),我不明白它需要什么Ord 要么。

【问题讨论】:

    标签: idris


    【解决方案1】:

    guard 单独不需要 Ord 约束,而是 (<) 在您的上一个问题中。我在那里给出了区分List (Ord b)Ord b => List b 的答案。

    要了解guard 为何抱怨缺少约束,请查看monad comprehensions 是如何脱糖的。

    [y | y <- xs, y < x] 变为 do {y <- xs; guard (y < x); pure y}

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-27
      • 1970-01-01
      • 2022-01-22
      • 2018-05-14
      • 1970-01-01
      • 1970-01-01
      • 2015-12-23
      • 1970-01-01
      相关资源
      最近更新 更多