在 Elm 中使用命名函数声明而不是匿名函数

Using named function declaration instead of anonymous function in Elm

在REPL中,如果我使用

> String.filter (\char -> char /= '-') "800-555-1234"

我得到结果:

"8005551234" : String

符合预期。

但是如果我使用这样的命名函数声明而不是匿名函数:

> String.filter (isKeepable char = char /= '-') "800-555-1234"

我收到这个错误:

-- SYNTAX PROBLEM -------------------------------------------- repl-temp-000.elm

The = operator is reserved for defining variables. Maybe you want == instead? Or
maybe you are defining a variable, but there is whitespace before it?

3|   String.filter (isKeepable char = char /= '-') "800-555-1234"
                                    ^
Maybe <http://elm-lang.org/docs/syntax> can help you figure it out.

这对我来说似乎很奇怪,因为函数声明本身是一个 returns 函数对象的表达式:

> isKeepable char = char /= '-'
<function> : Char -> Bool

那么为什么不能将该函数引用传递给 filter 就像任何计算结果为函数的表达式一样?

命名函数声明仅在顶层或 let 子句中有效。试试这个:

> let isKeepable char = char /= '-' in String.filter isKeepable "800-555-1234"