What does -> mean in F#?

后端 未结 9 1016
情深已故
情深已故 2020-12-03 02:54

I\'ve been trying to get into F# on and off for a while but I keep getting put off. Why?

Because no matter which \'beginners\' resource I try to look at I see very

9条回答
  •  有刺的猬
    2020-12-03 03:24

    First question - are you familiar with lambda expressions in C#? If so the -> in F# is the same as the => in C# (I think you read it 'goes to').

    The -> operator can also be found in the context of pattern matching

    match x with
    | 1 -> dosomething
    | _ -> dosomethingelse
    

    I'm not sure if this is also a lambda expression, or something else, but I guess the 'goes to' still holds.

    Maybe what you are really referring to is the F# parser's 'cryptic' responses:

    > let add a b = a + b
    val add: int -> int -> int
    

    This means (as most of the examples explain) that add is a 'val' that takes two ints and returns an int. To me this was totally opaque to start with. I mean, how do I know that add isn't a val that takes one int and returns two ints?

    Well, the thing is that in a sense, it does. If I give add just one int, I get back an (int -> int):

    > let inc = add 1
    val inc: int -> int
    

    This (currying) is one of the things that makes F# so sexy, for me.

    For helpful info on F#, I have found that blogs are FAR more useful that any of the official 'documentation': Here are some names to check out

    • Dustin Campbell (that's diditwith.net, cited in another answer)
    • Don Symes ('the' man)
    • Tomasp.net (aka Tomas Petricek)
    • Andrew Kennedy (for units of measure)
    • Fsharp.it (famous for the Project Euler solutions)
    • http://lorgonblog.spaces.live.com/Blog (aka Brian)
    • Jomo Fisher

提交回复
热议问题