21

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?

11
  • 4
    I don't know anything about F#, but I find the fact that it has a keyword named "fun".. funny Commented Dec 18, 2009 at 0:15
  • 4
    because coding in F# is fun! Its a friendly reminder while coding :)
    – Russell
    Commented Dec 18, 2009 at 0:15
  • 5
    If they took it out, there would be no fun in f#! Seriously though, that's curious as lambdas in C# don't need the keyword. It may be there just to simplify the parse. As languages evolve, these sorts of things tend to get reduced out/made optional.
    – Jim L
    Commented Dec 18, 2009 at 0:18
  • 2
    it's necessary if you want it to compile :)
    – dan
    Commented Dec 18, 2009 at 0:26
  • 1
    @yatima2975: I'm not really sure a backslash is so much less. It's a couple of characters shorter — otherwise they're identical. Both are tokens indicating a lambda.
    – Chuck
    Commented Dec 18, 2009 at 3:52

4 Answers 4

24

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?

4
  • 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?
    – rysama
    Commented Dec 18, 2009 at 0:25
  • 8
    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
    Commented Dec 18, 2009 at 0:29
  • 4
    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
    Commented Dec 18, 2009 at 0:32
  • 1
    @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
    Commented Dec 18, 2009 at 0:47
17

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

0

I just wanted to share that now it is possible (since f#8) to avoid the fun and lambdas altogether at least in a very specific situation: when your lambda is nothing more than an atomic expression (that means, when the expression in you lambda does not has any whitespace outside of a parentheses, like when you pass multiple curried arguments to a function.)

For example:

let exp1 = [{| X = 0; Y = 1; |}] |> List.distinctBy (fun x -> x.X)
let exp2 = ["A"; "B"; "C"] |> List.map (fun x -> x.ToLower())
let exp3 = List.map (fun x -> x.CompareTo(5)) [1..10]

Can now be written as:

let exp1 = [{| X = 0; Y = 1; |}] |> List.distinctBy _.X
let exp2 = ["A"; "B"; "C"] |> List.map _.ToLower()
let exp3 = [1..10] |> List.map _.CompareTo(5)

Notice that, in the case of exp3, I had to pass the data argument to the front and pipe the function, so the type inference knows that _ is an int in that context.

-1

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.

4
  • 2
    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
    Commented Dec 18, 2009 at 0:20
  • 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
    Commented Dec 18, 2009 at 0:22
  • 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
    Commented Dec 18, 2009 at 0:27
  • 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
    Commented Dec 18, 2009 at 0:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.