Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
459 views
in Technique[技术] by (71.8m points)

functional programming - What does -> mean in F#?

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 simple examples that start using the operator ->.

However, nowhere have I found as yet that provides a clear simple explanation of what this operator means. It's as though it must be so obvious that it doesn't need explanation even to complete newbies.

I must therefore be really dense or perhaps it's nearly 3 decades of previous experience holding me back.

Can someone please, explain it or point to a truly accessible resource that explains it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

'->' is not an operator. It appears in the F# syntax in a number of places, and its meaning depends on how it is used as part of a larger construct.

Inside a type, '->' describes function types as people have described above. For example

let f : int -> int = ...

says that 'f' is a function that takes an int and returns an int.

Inside a lambda ("thing that starts with 'fun' keyword"), '->' is syntax that separates the arguments from the body. For example

fun x y -> x + y + 1

is an expression that defines a two argument function with the given implementation.

Inside a "match" construct, '->' is syntax that separates patterns from the code that should run if the pattern is matched. For example, in

match someList with
| [] -> 0
| h::t -> 1

the stuff to the left of each '->' are patterns, and the stuff on the right is what happens if the pattern on the left was matched.

The difficulty in understanding may be rooted in the faulty assumption that '->' is "an operator" with a single meaning. An analogy might be "." in C#, if you have never seen any code before, and try to analyze the "." operator based on looking at "obj.Method" and "3.14" and "System.Collections", you may get very confused, because the symbol has different meanings in different contexts. Once you know enough of the language to recognize these contexts, however, things become clear.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...