2014-05-11 175 views
2
let tolerance = 0.00000001 
let (~=) x1 x2 = abs(x1 - x2) < tolerance 

這引發錯誤: 「無效的操作符定義前綴運營商定義必須使用有效的前綴運營商的名稱」爲什麼這個(〜=)被認爲是一個前綴運算符?

這甚至不是一個前綴操作符,我不明白爲什麼會是這樣認爲的。

然而,以下是罰款:

let (=~) x1 x2 = abs(x1 - x2) < tolerance 

我只是切換順序,以 「=」 來之前, 「〜」。

是否有任何文件在線陳述關於此的一些規則?

我正在使用Visual Studio 2013和「F#2013」​​。交互式控制檯顯示「F#Interactive version 12.0.21005.1」

回答

3

您不能在F#中定義以~開頭的中綴運算符。

F# 3.0 specification, section Categorization of Symbolic Operators解釋的原因很清楚:

The operators + , - , +. , -. , % , %% , & , && can be used as both prefix and infix operators. When these operators are used as prefix operators, the tilde character is prepended internally to generate the operator name so that the parser can distinguish such usage from an infix use of the operator. For example, -x is parsed as an application of the operator ~- to the identifier x . This generated name is also used in definitions for these prefix operators. Consequently, the definitions of the following prefix operators include the ~ character.

2

在F#中,第一個位置的~字符表示前綴運算符。例如,(~-)是前綴「相反」運算符:(~-) 3相當於- 3

+0

,所以你不能定義與開頭的管道符「〜」? –

+0

那麼沒有什麼可以擺脫波浪呢? – mbx

相關問題