简体   繁体   中英

Defining a function in f#

In F# I can define a function like this

let (-+->) x y = x + y

and call like this

let z =  5 -+-> 6

I can even do something like this

let (-++->) x y z = x y z
let p = ((fun x -> fun y -> x + y) -++-> 5 ) 6

But why can't I do this?

let (nafis) x y = x + y
let p = 5 nafis 6

it gives me this error Unexpected identifier in binding. Expected '=' or other token Unexpected identifier in binding. Expected '=' or other token .

Are functions like let (-+->) xy = x + y any special kind of function?

The lexical constraints on operators are defined in the F# specification:

3.7 Symbolic Operators

User-defined and library-defined symbolic operators are sequences of characters as shown below, except where the sequence of characters is a symbolic keyword(§3.6).

regexp first-op-char= !%&*+-./<=>@^|~
regexp op-char= first-op-char | ?
token quote-op-left = 
  | <@ <@@ 
token quote-op-right =  
  | @> @@> 
token symbolic-op= 
  | ?
  | ?<-
  | first-op-charop-char* 
  | quote-op-left
  | quote-op-right

as you can see, -++-> is valid, while nafis is not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM