Practice with examples F # (iii)--understanding functions in functional languages

Source: Internet
Author: User
Tags definition sin

In the first two, I said some basic grammar in F #, today I'm going to talk about functions, functions that play a very important role in functional programming, and, to put it simply, if you know and can use the function skillfully, you can say "I'm proficient in functional programming."

There are often people who think F # is difficult to use, and I think part of it is the functional interface in F # (the interface here refers to function signature, which I'm used to call a functional interface, and forgive me if it's inconvenient for you to read). It looks very different from what we normally know (such as C #), which leads some friends to encounter difficulties in the early stage, and then find it difficult to understand, and finally put F # into the doghouse. Hopefully, the following will make it feel like F # 's functions are no longer difficult to understand.

Or the old rules, let's take a look at an example

1 Let res = Seq.unfold (Fun (a,b)->some (A, (A+b,a))) (0,1)

2 Seq.iter (fun x-> printf "%d" x) (Seq.take res)

(This example uses a basic immutable type seq< ' T> in F #, and you can simply equate it to the famous ienumerable<t> in C # 3.0, because this is not the focus of this article, so omit the table)

This code is the result of the run

0 1 1 2 3 5 8 13 21 34 (note that after the second line, I only took the first 10 elements of sequence, Seq.take res). See the output, I think we all understand that the first line of the program is to generate the Fibonacci series, let's look at the SEQ.UNF The interface of the old method

Val unfold: (' State-> (' T * ') option)-> ' state-> seq< ' t>

I think a lot of people in the beginning of learning F # have seen such a function interface description will feel very unfamiliar with it, it does not matter, this is the purpose of this article, so that you are no longer unfamiliar with it.

F # uses the arrow "->" to mark a function interface, such as an int-> string, which means that it is a function that takes an int parameter and returns a string value. For example let f (x:int) = X.tostring ()

Understanding the interface of this image does not help us to understand the Seq.unfold method, we also need to understand some basic concepts.

Higher-order Function. It is possible that you have not heard of this before, but don't be surprised by this strange name, in fact, we all in the middle school have contact with this thing, do not believe you see this

Sin (x+y) = Sinxcosy+cosxsiny. Triangle and angle formula, this everybody should have some memory? Someone has to ask what does this have to do with Higher-order function? Don't worry, let's look at the definition of Higher-order function first. SICP is defined in this way: procedures that manipulate procedures are called. (The procedures in the definition is the meaning of the function we are talking about here). The operation mentioned in the definition (manipulate) function is not let us fantasize, how to operate the function? It is much simpler to understand a function as a normal parameter (the simple point is that a function accepts an argument that can be a function, and its return value can be a function). Then why do you call Higher-order procedures? Your boss can make you do this all day long do you think you can turn your boss around? J look at me above the triangle and angle formula, the sin and Cos are regarded as functions, hehe, are we in high school have contact with Higher-order function?

With the concept of higher-order function, it seems that we cannot quickly see how seq.unfold should be used. Let's take a look at a simple function

Let add x y = x+y (we see the interface of this function is Val add:int-> int-> int)

Let's go over this simple method, in F #, the arrow "->" to represent a function, and it is a combination of right to left, so we can think of Int->int->int as Int-> (Int->int), With the Higher-order function just mentioned, this becomes easy to understand, add accepts an int parameter and returns a Int->int function. Let's rewrite this with the add to make it more intuitive.

Let add x y = (fun x-> (fun y-> x+y)) (This is easy to read, right?) Takes an argument x, returns a function fun y-> x+y, the returned function takes a parameter y, and returns the X+y value. )

With the above foundation, let's take it a step further,

Let ADD10 = Add 10

I think we can all see it, let Add10 = (fun x-> (fun y-> x+y)) = Fun y-> 10+y. Similar to the use of ADD10, which is called currying fuction in F #, this Curry has nothing to do with Curry, which, like the Haskell language, is to commemorate the famous logic expert Haskell Curry, of course currying function is not F # alone, you can actually see it in almost any functional language.

So much has been said, let's go back to the first example,

Val unfold: (' State-> (' T * ') option)-> ' state-> seq< ' t>

Now this looks less difficult than before, the unfold method accepts a parameter (' state-> (') option ') and returns a function that accepts ' state and returns seq< ' t>. What are the parameters that are accepted (' state-> (' T * ' state) option? is of course a function that accepts the ' state ' parameter and returns (' T * ' state) option. Then we use a stupid way to understand let res = Seq.unfold (Fun (a,b)->some (A, (A+b,a))) (0,1) One by one corresponds, (a,b) corresponds to ' state, Tuple (A, (a+b,a)) The "a" in front of the comma corresponds to ' t, and the following (A+b,a) corresponds to ' t* ' state, because we see that the interface description is (' T * ') option, so we add the Some keyword accordingly (for option type, see previous), I don't explain the latter part much, and by one by one, we see that the value in SEQ is the first item in tuple (A, (A+b,a)), which is a before the comma. Do you understand how to read a function interface?

If you are just starting to learn F #, do a few exercises to solidify the knowledge you have learned today? Can you construct a function based on the function interface written below?

1. ' A-> (' a-> ' B)-> ' B

2. (' a-> ' B)-> (' C-> ' a)-> ' C-> ' b

3. (' t-> bool)-> ' t list-> ' t list

Summarize:

1. Every function in F # has a return value that can be either a specific value or another function (the unit indicates that the function return value is empty (void)). When reading the description of a function interface, the section following the rightmost arrow "->" represents the return value of the function. Every function in F # can only accept one argument, and the same argument can be either a specific value or a parameter.

2. Higher-order function and currying function is an important cornerstone of understanding and proficiency in functional programming, and I hope that a friend who is not quite clear can understand the example above and lay the groundwork well.

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.