What does -> mean in F#?

后端 未结 9 1017
情深已故
情深已故 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
    0 讨论(0)
  • 2020-12-03 03:29

    From Microsoft:

    Function types are the types given to first-class function values and are written int -> int. They are similar to .NET delegate types, except they aren't given names. All F# function identifiers can be used as first-class function values, and anonymous function values can be created using the (fun ... -> ...) expression form.

    0 讨论(0)
  • 2020-12-03 03:31

    It basically means "maps to". Read it that way or as "is transformed into" or something like that.

    So, from the F# in 20 minutes tutorial,

    > List.map (fun x -> x % 2 = 0) [1 .. 10];;
    val it : bool list
    = [false; true; false; true; false; true; false; true; false; true]
    

    The code (fun i -> i % 2 = 0) defines an anonymous function, called a lambda expression, that has a parameter x and the function returns the result of "x % 2 = 0", which is whether or not x is even.

    0 讨论(0)
  • 2020-12-03 03:35

    There are plenty of great answers here already, I just want to add to the conversation another way of thinking about it.

    ' -> ' means function.

    'a -> 'b is a function that takes an 'a and returns a 'b

    ('a * 'b) -> ('c * 'd) is a function that takes a tuple of type ('a, 'b) and returns a tuple of ('c, 'd). Such as int/string returns float/char.

    Where it gets interesting is in the cascade case of 'a -> 'b -> 'c. This is a function that takes an 'a and returns a function ('b -> 'c), or a function that takes a 'b -> 'c.

    So if you write: let f x y z = ()

    The type will be f : 'a -> 'b -> 'c -> unit, so if you only applied the first parameter, the result would be a curried function 'b -> 'c -> 'unit.

    0 讨论(0)
  • 2020-12-03 03:35

    In the context of defining a function, it is similar to => from the lambda expression in C# 3.0.

    F#: let f = fun x -> x*x
    C#: Func<int, int> f = x => x * x;
    

    The -> in F# is also used in pattern matching, where it means: if the expression matches the part between | and ->, then what comes after -> should be given back as the result:

    let isOne x = match x with
     | 1 -> true
     | _ -> false
    
    0 讨论(0)
  • 2020-12-03 03:36

    Many great answers to this questions, thanks people. I'd like to put here an editable answer that brings things together.

    For those familiar with C# understanding -> being the same as => lamba expression is a good first step. This usage is :-

    fun x y -> x + y + 1
    

    Can be understood as the equivalent to:-

    (x, y) => x + y + 1;
    

    However its clear that -> has a more fundemental meaning which stems from concept that a function that takes two parameters such as the above can be reduced (is that the correct term?) to a series of functions only taking one parameter.

    Hence when the above is described in like this:-

    Int -> Int -> Int
    

    It really helped to know that -> is right associative hence the above can be considered:-

    Int -> (Int -> Int)
    

    Aha! We have a function that takes Int and returns (Int -> Int) (a curried function?).

    The explaination that -> can also appear as part of type definiton also helped. (Int -> Int) is the type of any of function which takes an Int and returns an Int.

    Also helpful is the -> appears in other syntax such as matching but there it doesn't have the same meaning? Is that correct? I'm not sure it is. I suspect it has the same meaning but I don't have the vocabulary to express that yet.

    Note the purpose of this answer is not to spawn further answers but to be collaboratively edited by you people to create a more definitive answer. Utlimately it would be good that all the uncertainies and fluf (such as this paragraph) be removed and better examples added. Lets try keep this answer as accessible to the uninitiated as possible.

    0 讨论(0)
提交回复
热议问题