F # ≥c # (tuple and swap)

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.