F#: Is the "fun" keyword necessary?
I was typing the "fun" keyword and then I remembered you don't have to in C#
Wouldn't this:
List.map (x -> x + 1) [1..10]
Be just as expressive as this?:
List.map (fun x -> x + 1) [1..10]
This makes me curious as to why the "fun" keyword is necessary at all. Can someone clarify why the "fun" keyword is syntacti开发者_如何转开发cally required?
The language is ambiguous without it.
let x y = y z -> y z
Does x
call y
on the function z -> y z
or does it ignore its argument and return the function y z -> y z
?
Lots of decent speculative answers already... I'll add to the mix:
F# has a core language that's compatible with OCaml, and OCaml uses 'fun'.
I know as part of currying, (see this post) you can replace:
let countOneToTen = fun y List.map(fun x -> x + 1) y
countOneToTen = [1..10]
with
let countOneToTen y = List.map(fun x -> x + 1) y
countOneToTen = [1..10]
without the fun keyword.
精彩评论