2017-10-04 178 views
1

在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. 

這似乎很奇怪,對我來說,因爲函數聲明本身是一個返回函數對象的表達式:

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

那麼爲什麼不能這麼說像任何表達式一樣,將參數傳遞給filter

回答

4

命名函數聲明只在頂級或let子句中有效。試試這個:

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