Difference between revisions of "Curried Function"

From Conservapedia
Jump to navigation Jump to search
(→‎An Example of Why and How: Spelling/Grammar Check, typos fixed: For example → For example,, defintion → definition)
 
Line 14: Line 14:
 
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.
 
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.
+
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
 
In Haskell syntax we would define this as
Line 21: Line 21:
 
  multiply x y = x * y
 
  multiply x y = x * y
  
With this new defintion we can just set out function double to be multiply 2. In Haskell this is
+
With this new definition we can just set out function double to be multiply 2. In Haskell this is
  
 
  double :: Integer -> Integer
 
  double :: Integer -> Integer

Latest revision as of 14:48, July 15, 2016

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 definition 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.