Is the following code really executed in haskell?

I try to understand currying by reading various blogs and adding up responses to the stream, and I think I understood something. In Haskell, every function has curry, which means when you have a function, for example, fxy = x + y
it's really
((fx) y)
in this case, the function first takes the first parameter "x" as the parameter and partially applies it to the function f, which in turn returns the function for y. where it takes only y one parameter and applies this function. In both cases, the function accepts only one parameter, and the process of decreasing the function to accept one parameter is called "currying". Correct me if my understanding is wrong. So, if this is correct, could you tell me if the two and three functions are curry functions?

 three xyz = x + y + z two = three 1 same = two 1 

In this case, I have two specialized functions: “two” and “identical”, which boil down to taking only one parameter, so what is curry?

+5
source share
3 answers

First, consider two . He has a signature

 two :: Num a => a -> a -> a 

forget Num a at the moment (this is only a restriction on a - here you can read Int ). Surely this is also a curry function.

The following is more interesting:

 same :: Num a => a -> a 

(btw: nice name is the same, but not exactly id ^^)

TBH: I don't know for sure.

The best definition I know about carding function is:

A curried function is a function of N arguments that return another function (N-1) of arguments.

(if you want, you can, of course, extend this to fully traced functions)

This will only match if you define constants as functions with 0 parameters, which you certainly can. Thus, I would say that yes (?) This is also a currency function, but only in the mathematical boundary mode (for example, the sum of 0 numbers is defined as 0)

+4
source

It’s best to think about it equatorially. The following are all equivalent definitions:

 fxyz = x+y+z fxy = \z -> x+y+z fx = \y -> (\z -> x+y+z) f = \x -> (\y -> (\z -> x+y+z)) 

Partial application is only related here. More often than not, you do not want the actual partial application to run, and the actual lambda object to be created in memory - hoping that the compiler will use and optimize better - the full definition at the endpoint of the complete application.

Having curry / uncurry is another confusing issue. Both f (x,y) = ... and fxy = ... , of course, are in Haskell, but in our heads we tend to think of the first as a function of two arguments, therefore functions that translate between two forms are called curry and uncurry , like mnemonics.

+4
source

You may think that the function is three with anonymous functions:

 three = \x -> (\y -> (\z -> x + y + z))) 
+1
source

Source: https://habr.com/ru/post/1202564/


All Articles