haskell - haskell (ghci) 中的点自由编程风格

标签 haskell pointfree

我在 haskell 中看到一段用于计算组合的代码,我想我会在 ghci 中使用它来了解它是如何工作的。在 ghci 中,当我只运行我感兴趣的代码时,它会失败,如下所示。

Prelude Data.List> filter ((== 2) . length) . subsequences [1,2,3]

<interactive>:19:1:
    No instance for (Show ([a0] -> [[a0]]))
      arising from a use of ‘print’
    In a stmt of an interactive GHCi command: print it
Prelude Data.List> filter ((== 2) . length) . subsequences [1,2,3]

<interactive>:20:28:
    Couldn't match expected type ‘a -> [[a1]]’
                with actual type ‘[[a0]]’
    Relevant bindings include
      it :: a -> [[a1]] (bound at <interactive>:20:1)
    Possible cause: ‘subsequences’ is applied to too many arguments
    In the second argument of ‘(.)’, namely ‘subsequences [1, 2, 3]’
    In the expression:
      filter ((== 2) . length) . subsequences [1, 2, 3]

我知道这一定是因为 composition(dot) 运算符。在 ghci 中,我不得不将其更改为使用“$”符号来运行该代码,如下所示

Prelude Data.List> filter ((== 2) . length) $ subsequences [1,2,3]
[[1,2],[1,3],[2,3]]

有人可以解释一下当您以这种编程风格(pointfree)编写代码时内部发生了什么吗?为什么当我直接使用该表达式时 ghci 会失败?

最佳答案

这只是运算符优先级的问题。您的代码被读取为

(filter ((== 2) . length))    .     (subsequences [1,2,3])

然后 GHCi 提示 subsequences [1,2,3] 不是函数。所以,你需要括号:

(filter ((== 2) . length) . subsequences) [1,2,3]

$:

filter ((== 2) . length) . subsequences $ [1,2,3]

关于haskell - haskell (ghci) 中的点自由编程风格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29100665/

相关文章:

linux - GHCi - Haskell 编译器错误 -/home/user/.ghci 归其他人所有,忽略

haskell - 如何用递归判断一个数是否是平方数?

haskell - 将递归函数重写为管道函数组合

haskell - 可变参数组合函数?

haskell - 无点形式与风格

haskell - 使数字函数成为 Num 的实例?

haskell - 容器元素类型

haskell - 关于懒惰的困惑

haskell - 向左折叠操作的无点功能

F#点自由风格划分