Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Currying is a transformation that can be applied to functions to allow them to take one less argument than previously.</p> <p>For example, in F# you can define a function thus:-</p> <pre><code>let f x y z = x + y + z </code></pre> <p>Here function f takes parameters x, y and z and sums them together so:-</p> <pre><code>f 1 2 3 </code></pre> <p>Returns 6.</p> <p>From our definition we can can therefore define the curry function for f:-</p> <pre><code>let curry f = fun x -&gt; f x </code></pre> <p>Where 'fun x -> f x' is a lambda function equivilent to x => f(x) in C#. This function inputs the function you wish to curry and returns a function which <em>takes a single argument</em> and returns the specified function with the first argument set to the input argument.</p> <p>Using our previous example we can obtain a curry of f thus:-</p> <pre><code>let curryf = curry f </code></pre> <p>We can then do the following:-</p> <pre><code>let f1 = curryf 1 </code></pre> <p>Which provides us with a function f1 which is equivilent to f1 y z = 1 + y + z. This means we can do the following:-</p> <pre><code>f1 2 3 </code></pre> <p>Which returns 6.</p> <p>This process is often confused with 'partial function application' which can be defined thus:-</p> <pre><code>let papply f x = f x </code></pre> <p>Though we can extend it to more than one parameter, i.e.:-</p> <pre><code>let papply2 f x y = f x y let papply3 f x y z = f x y z etc. </code></pre> <p>A partial application will take the function and parameter(s) and return a function that requires one or more less parameters, and as the previous two examples show is implemented directly in the standard F# function definition so we could achieve the previous result thus:-</p> <pre><code>let f1 = f 1 f1 2 3 </code></pre> <p>Which will return a result of 6.</p> <p>In conclusion:-</p> <p>The difference between currying and partial function application is that:-</p> <p>Currying takes a function and provides a new function accepting a single argument, and returning the specified function with its first argument set to that argument. <em>This allows us to represent functions with multiple parameters as a series of single argument functions</em>. Example:-</p> <pre><code>let f x y z = x + y + z let curryf = curry f let f1 = curryf 1 let f2 = curryf 2 f1 2 3 6 f2 1 3 6 </code></pre> <p>Partial function application is more direct - it takes a function and one or more arguments and returns a function with the first n arguments set to the n arguments specified. Example:-</p> <pre><code>let f x y z = x + y + z let f1 = f 1 let f2 = f 2 f1 2 3 6 f2 1 3 6 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload