In F #, there is a concept called tuple. "tuples" are groups of untitled but sorted values that may have different types, this article compares it with swap in C.
F #, as an emerging language, has a simpler implementation method than C #, which can make the code more concise and reduce program bugs, this is why I like F # so much. Next we will give some examples to describe what F # does better than C #. Today we will talk about the difference between tuples and exchanges.
Swap is the simplest operation that I can think of. Now, I want a common exchange method that can handle any type. in C, you can use the object type or generic type, but in any case, you must use a temporary variable (although some methods can do this without using temporary variables, however, F # can easily implement this function:
Let swap (a, B) = (B,)
The passed parameter is (a, B) And then we can directly get the return value (B, A). Isn't that simple?
The only thing that puzzles me is that (a, B) is a special type, which is a tuple. Therefore, you can understand it as follows, when implementing the switching function, you only need a single parameter, which is the tuples.
Tuples also support more than two parameters. In this way, you can perform the following exchanges:
Let swap2 (a, B, c, d, e) = (A, C, B, E, D)
At the same time, you can perform any exchange based on your own wishes in the tuples.
Note: This article is a translation, if you are interested, please refer to the original article of the blogger Blogspot: http://apollo13cn.blogspot.com/2011/11/f-c-tuple-and-swap.html