2,081 bytes added
, 04:43, September 20, 2011
A '''Curried Function''' is a concept in [[Computer Science]] and [[programming]] related to how a function takes in an argument and gives out a value. It is named for the logician Haskell Curry.
==An Example of Why and How==
Imagine we have a [[function]] that multiplies two numbers together, call it ''multiply''. Clearly it must take two numbers as an argument (we'll just stick with [[integers]] for simplicity).. However we're restricted by functional languages which only let us give one value as input to a function. The obvious answer is to package the two numbers together as a new value, called a [[tuple]]. This function would be written in Haskell as
multiply :: (Integer,Integer) -> Integer
multiply (x,y) = x * y
Now let's say we multiply things by two a lot in our program, so rather than writing this out repeatedly we'll define a new function, which we'll call ''double''. We want to use our old function as we've already defined it (obviously it's not too much effort with multiplication, but the principles apply to complicated functions as well).
However we have a problem, there isn't a way of doing this. We don't have a tuple to give multiply. However, imagine if we defined multiply a new way.
Rather than giving multiply a tuple with two numbers, what if we only gave multiply one number. This function would then return another function, which took another number. This second function would then return the answer.
For example if we were multiplying 3 and 2, multiply would take 3, return a new function called ''multiply 3'' which takes 2 as its argument and returns 6.
In Haskell syntax we would define this as
multiply :: Integer -> Integer -> Integer
multiply x y = x * y
With this new defintion we can just set out function double to be multiply 2. In Haskell this is
double :: Integer -> Integer
double x = multiply 2 x
or if preferred
double = multiply 2
This latter style is called [[points free programming]].
This is called partial function application and is one of the greatest strengths of [[functional programming]].