【问题标题】:Haskell dot (.) operator in de Morgan's law implementation德摩根定律实施中的 Haskell 点 (.) 运算符
【发布时间】:2017-05-29 21:08:52
【问题描述】:

this question,作者在 Haskell 中编写了德摩根定律的实现。我了解notAandnotBnotAornotB 的实现,但我很难理解notAorB 的实现,即:

notAorB :: (Either a b -> c) -> (a -> c, b -> c)
notAorB f = (f . Left, f . Right)

有人能解释一下(f . Left, f . Right) 部分的工作原理吗?我以前见过. 运算符,但它有三个参数,而不是两个。

提前谢谢你。

【问题讨论】:

    标签: haskell demorgans-law dot-operator


    【解决方案1】:

    回想. 运算符的定义是(f . g) x = f (g x)ie f . g = \x -> f (g x)(在语法上,它是一个二元运算符,只是Haskell 的语法糖允许将后一个定义重述为前一个)。所以,你的定义可以改写为

    notAorB f = ((\x -> f (Left x)), (\y -> f (Right y)))
    

    (这可以通过Lambdabot#haskell 机械地完成,告诉他@unpl ‹expr›),或者更冗长

    notAorB f = (lt, rt)
      where lt x = f (Left x)
            rt y = f (Right y)
    

    和往常一样,试着写下类型。如果(.) :: ∀ s t u. (t -> u) -> (s -> t) -> s -> uf :: Either a b -> cLeft :: ∀ p q. p -> Either p q,那么f . Left(.) f Left :: a -> c等等。

    【讨论】:

      猜你喜欢
      • 2016-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-10
      • 2016-08-29
      • 1970-01-01
      • 1970-01-01
      • 2017-07-16
      相关资源
      最近更新 更多