tags:

views:

298

answers:

5

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 syntactically required?

+1  A: 

I think it makes it more understandable for people new to the language. However, it is a bit useless (from my small bit of experience with the language).

Although it is a great way to convey how you feel while writing code.

Tyler Smith
A: 

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.

Russell
That's not what the OP is talking about. They are instead asking why `fun x -> x + 1` can't just be `x -> x + 1`.
bcat
The question is asking whether "the 'fun' keyword is necessary at all," I was giving another example of where it can be used and omitted depending on developer preference.
Russell
I know (and I'm not the one who downvoted you, to be clear). Although I honestly don't know why anyone would prefer the version that doesn't take advantage of partial application. :)
bcat
I see where you are coming from @bcat. This is a scenario where I would use the partial application of it as well, however I was talking more general. I find (assuming the functions are named correctly), currying functions (apart from reducing no. param values) provides a clearer flow of code. (*Note: I am still learning F# too! :) )
Russell
+10  A: 

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?

Chuck
hmm good point, but surely you could add parenthesis to solve the ambiguity: "y (z -> y z)"? hmm... would that just end up making things more verbose?
RodYan
But then you've just transformed `fun` into `()`. C# can get away with not having a special syntax for lambdas because it has a more elaborate syntax to begin with. In a language like F#, I don't see how you can get around using some kind of delimiter to say "OK, lambda coming up!"
Chuck
I don't think this has to be ambiguous, most languages have tons of ambiguity, and there are precedence rules that are used to 'break' the ambiguity. This could possibly be another such case.
Brian
@Brian: I'm trying to think of how such a rule would be expressed without making other parts of the language more confusing. It seems like the only simple choices are "every identifier before a `->` is an argument" (the "turn `fun` into `()`" option) or "lambdas only take one argument" (which I guess would bring us closer to lambda calculus). There's probably another option I haven't thought of, but I do think `fun` is a pretty simple solution here.
Chuck
Wow, 2 downvotes without comment in quick succession. Did the jerks just get out of school or what?
Chuck
+1  A: 

I think you're not taking the long-term view. If they start removing all the unnecessary syntax bits, F# will turn in to Haskell, and nobody wants that...right?

Daniel Pratt
Haskell has a lambda token as well. fun = \
Chuck
I know, but I never let the facts prevent me from making a bad joke.
Daniel Pratt
+5  A: 

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

Brian