Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of the Application of function Adrigenization and partial function in C language

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor will share with you the relevant knowledge points of C language function Gary and partial function application example analysis, the content is detailed, the logic is clear, and I believe that most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article, let's take a look at it.

[noun explanation] Currying: because it was the American mathematical logician Haskell Curry who invented the technique of using this function, the usage was named Currying after his name and translated as "Gary" in Chinese.

I feel that many people are confused about the difference between function Currying and partial function applications (Partial Application), especially when they occur at the same time in similar contexts.

Partial functions solve the problem that if we have a function with multiple parameters, we want to be able to fix the values of some of the parameters.

There are obvious applications of partial functions in almost all programming languages. In C language:

Int foo (int a, int b, int c) {return a + b + c;} int foo23 (int a, int c) {return foo (a, 23, c);}

The foo23 function is actually a partial function application of the foo function, and the value of parameter b is fixed to 23.

Of course, obvious partial functions like this are not very useful; we usually want programming languages to provide us with some partial function features.

For example, in the Python language, we can do this:

From functools import partial def foo: return a + b + c foo23 = partial (foo, bau23) foo23 (a = 1, c = 3) # = > 27

Function Currying obviously solves a completely different problem: if we have several single-parameter functions and this is a language that supports first-class functions (first-class), how do we implement a multi-parameter function? Function Carrialization is a method to realize multi-parameter function.

Here is a single-argument Javascript function:

Var foo = function (a) {return a * a;}

If we are limited to writing a single-parameter function, we can simulate a multi-parameter function like this:

Var foo = function (a) {return function (b) {return a * a + b * b;}}

Call it by either (foo (3)) (4) or directly foo (3) (4).

Note that function Gary provides a very natural way to implement some partial function applications. If you want the parameter values of the function foo to be fixed to 5, all you need to do is var foo5 = foo (5). This is OK. The function foo5 is the partial function of the foo function. Note, however, that there is no easy way to partial functionalize the second parameter of the foo function (unless you first partial the arguments).

Of course, Javascript supports multi-parameter functions:

Var bar = function (a, b) {return a * a + b * b;}

The bar function we define is not a garilized function. Calling bar (5) does not return a function that can enter 12. We can only call this function like bar (5, 5, 12).

In some other languages, such as Haskell and OCaml, all multiparameter functions are implemented through Gary.

Here is an example of writing the above foo function in OCaml:

Let foo = fun a-> fun b-> a * a + b * b

Here is an example of writing the above bar function in OCaml:

Let bar = fun a b-> a * a + b * b

The first function is called "explicit Carrialization" and the second function is called "implicit Galerization".

Unlike Javascript, in OCaml, the foo function is exactly the same as the bar function. We call them in exactly the same way.

# foo 3 4;: int = 25 # bar 3 4; -: int = 25

Both functions can create a partial function by providing a parameter value:

# let foo5 = foo5;; val foo5: int-> int = # let bar5 = bar5;; val bar5: int-> int = # foo5 12; -: int = 169 # bar5 12; -: int = 169

In fact, we can put the following anonymous function:

Fun arg1 arg2... ArgN-> exp

As an acronym for the following function:

Fun arg1-> fun arg2->...-> fun argN-> exp is all the contents of the article "C language function Gary and partial function Application example Analysis". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report