<p>Anyone have a decent example, preferably practical/useful, they could post demonstrating the concept?</p>

[{'answer_id': 8457, 'author': 'Patrick', 'author_id': 429, 'author_profile': 'https://Stackoverflow.com/users/429', 'pm_score': 2, 'selected': False, 'text': '<p>It\'s a fairly simple process. Take a function, bind one of its arguments and return a new function. For example:</p>\n\n<pre><code>let concatStrings left right = left + right\nlet makeCommandPrompt= appendString "c:\\&gt; "\n</code></pre>\n\n<p>Now by currying the simple concatStrings function, you can easily add a DOS style command prompt to the front of any string! Really useful!</p>\n\n<p>Okay, not really. A more useful case I find is when I want to have a make a function that returns me data in a stream like manner.</p>\n\n<pre><code>let readDWORD array i = array[i] | array[i + 1] &lt;&lt; 8 | array[i + 2] &lt;&lt; 16 | \n    array[i + 3] &lt;&lt; 24 //I\'ve actually used this function in Python.\n</code></pre>\n\n<p>The convenient part about it is that rather than creating an entire class for this sort of thing, calling the constructor, calling obj.readDWORD(), you just have a function that can\'t be mutated out from under you.</p>\n'}, {'answer_id': 8468, 'author': 'nlucaroni', 'author_id': 157, 'author_profile': 'https://Stackoverflow.com/users/157', 'pm_score': 5, 'selected': True, 'text': '<blockquote>\n  <p>(Edit: a small <a href="http://web.archive.org/web/20041012103936/http%3A//www.bagley.org/~doug/ocaml/Notes/okoans.shtml" rel="nofollow noreferrer">Ocaml FP Koan</a> to start things off)</p>\n  \n  <blockquote>\n    <p><strong>The Koan of Currying (A koan about food, that is not about food)</strong></p>\n    \n    <blockquote>\n      <p>A student came to Jacques Garrigue and said, "I do not understand what currying is good for." Jacques replied, "Tell me your favorite meal and your favorite dessert". The puzzled student replied that he liked okonomiyaki and kanten, but while his favorite restaurant served great okonomiyaki, their kanten always gave him a stomach ache the following morning. So Jacques took the student to eat at a restaurant that served okonomiyaki every bit as good as the student\'s favorite, then took him across town to a shop that made excellent kanten where the student happily applied the remainder of his appetite. The student was sated, but he was not enlightened ... until the next morning when he woke up and his stomach felt fine. </p>\n    </blockquote>\n  </blockquote>\n</blockquote>\n\n<p>My examples will cover using it for the reuse and encapsulation of code. This is fairly obvious once you look at these and should give you a concrete, simple example that you can think of applying in numerous situations.</p>\n\n<p>We want to do a map over a tree. This function could be curried and applied to each node if it needs more then one argument -- since we\'d be applying the one at the node as it\'s final argument. It doesn\'t have to be curried, but writing <em>another</em> function (assuming this function is being used in other instances with other variables) would be a waste.</p>\n\n<pre><code>type \'a tree = E of \'a | N of \'a * \'a tree * \'a tree\nlet rec tree_map f tree = match tree with\n    | N(x,left,right) -&gt; N(f x, tree_map f left, tree_map f right)\n    | E(x) -&gt; E(f x)\n\nlet sample_tree = N(1,E(3),E(4)\nlet multiply x y = x * y\nlet sample_tree2 = tree_map (multiply 3) sample_tree\n</code></pre>\n\n<p>but this is the same as:</p>\n\n<pre><code>let sample_tree2 = tree_map (fun x -&gt; x * 3) sample_tree\n</code></pre>\n\n<p>So this simple case isn\'t convincing. It really is though, and powerful once you use the language more and naturally come across these situations. The other example with some code reuse as currying. A <a href="https://stackoverflow.com/questions/7272/of-ways-to-count-the-limitless-primes#7487">recurrence relation to create prime numbers</a>. Awful lot of similarity in there:</p>\n\n<pre><code>let rec f_recurrence f a seed n =\n    match n with\n    | a -&gt; seed\n    | _ -&gt; let prev = f_recurrence f a seed (n-1) in\n           prev + (f n prev)\n\nlet rowland = f_recurrence gcd 1 7\nlet cloitre = f_recurrence lcm 1 1\n\nlet rowland_prime n = (rowland (n+1)) - (rowland n)\nlet cloitre_prime n = ((cloitre (n+1))/(cloitre n)) - 1\n</code></pre>\n\n<p>Ok, now rowland and cloitre are curried functions, since they have free variables, and we can get any index of it\'s sequence without knowing or worrying about f_recurrence.</p>\n'}, {'answer_id': 9012, 'author': 'Chris Smith', 'author_id': 322, 'author_profile': 'https://Stackoverflow.com/users/322', 'pm_score': 4, 'selected': False, 'text': '<p>While the previous examples answered the question, here are two simpler examples of how Currying can be beneficial for F# programming.</p>\n\n<pre><code>open System.IO\n\nlet appendFile (fileName : string) (text : string) =\n    let file = new StreamWriter(fileName, true)\n    file.WriteLine(text)\n    file.Close()\n\n// Call it normally    \nappendFile @"D:\\Log.txt" "Processing Event X..."\n\n// If you curry the function, you don\'t need to keep specifying the\n// log file name.\nlet curriedAppendFile = appendFile @"D:\\Log.txt"\n\n// Adds data to "Log.txt"\ncurriedAppendFile "Processing Event Y..."\n</code></pre>\n\n<p>And don\'t forget you can curry the Printf family of function! In the curried version, notice the distinct lack of a lambda.</p>\n\n<pre><code>// Non curried, Prints 1 2 3 \nList.iter (fun i -&gt; printf "%d " i) [1 .. 3];;\n\n// Curried, Prints 1 2 3\nList.iter (printfn "%d ") [1 .. 3];;\n</code></pre>\n'}, {'answer_id': 2800182, 'author': 'J D', 'author_id': 13924, 'author_profile': 'https://Stackoverflow.com/users/13924', 'pm_score': 2, 'selected': False, 'text': '<p>You know you can map a function over a list? For example, mapping a function to add one to each element of a list:</p>\n\n<pre><code>&gt; List.map ((+) 1) [1; 2; 3];;\nval it : int list = [2; 3; 4]\n</code></pre>\n\n<p>This is actually already using currying because the <code>(+)</code> operator was used to create a function to add one to its argument but you can squeeze a little more out of this example by altering it to map the same function of a list of lists:</p>\n\n<pre><code>&gt; List.map (List.map ((+) 1)) [[1; 2]; [3]];;\nval it : int list = [[2; 3]; [4]]\n</code></pre>\n\n<p>Without currying you could not partially apply these functions and would have to write something like this instead:</p>\n\n<pre><code>&gt; List.map((fun xs -&gt; List.map((fun n -&gt; n + 1), xs)), [[1; 2]; [3]]);;\nval it : int list = [[2; 3]; [4]]\n</code></pre>\n'}, {'answer_id': 6221396, 'author': 'Michael Brown', 'author_id': 14359, 'author_profile': 'https://Stackoverflow.com/users/14359', 'pm_score': 1, 'selected': False, 'text': '<p>I gave a good example of simulating currying in C# <a href="http://azurecoding.net/blogs/brownie/archive/2010/01/18/accounting-for-cost-of-sales-with-currying.aspx" rel="nofollow">on my blog</a>. The gist is that you can create a function that is closed over a parameter (in my example create a function for calculating the sales tax closed over the value of a given municipality)out of an existing multi-parameter function.</p>\n\n<p>What is appealing here is instead of having to make a separate function specifically for calculating sales tax in Cook County, you can create (and reuse) the function dynamically at runtime.</p>\n'}, {'answer_id': 14785251, 'author': 'phoog', 'author_id': 385844, 'author_profile': 'https://Stackoverflow.com/users/385844', 'pm_score': 3, 'selected': False, 'text': '<p>Currying describes the process of transforming a function with multiple arguments into a chain of single-argument functions.  Example in C#, for a three-argument function:</p>\n\n<pre><code>Func&lt;T1, Func&lt;T2, Func&lt;T3, T4&gt;&gt;&gt; Curry&lt;T1, T2, T3, T4&gt;(Func&lt;T1, T2, T3, T4&gt; f)\n{\n    return a =&gt; b =&gt; c =&gt; f(a, b, c);\n}\n\nvoid UseACurriedFunction()\n{\n    var curryCompare = Curry&lt;string, string, bool, int&gt;(String.Compare);\n    var a = "SomeString";\n    var b = "SOMESTRING";\n    Console.WriteLine(String.Compare(a, b, true));\n    Console.WriteLine(curryCompare(a)(b)(true));\n\n    //partial application\n    var compareAWithB = curryCompare(a)(b);\n    Console.WriteLine(compareAWithB(true));\n    Console.WriteLine(compareAWithB(false));\n}\n</code></pre>\n\n<p>Now, the boolean argument is probably <em>not</em> the argument you\'d most likely want to leave open with a partial application.  This is one reason why the order of arguments in F# functions can seem a little odd at first.  Let\'s define a different C# curry function:</p>\n\n<pre><code>Func&lt;T3, Func&lt;T2, Func&lt;T1, T4&gt;&gt;&gt; BackwardsCurry&lt;T1, T2, T3, T4&gt;(Func&lt;T1, T2, T3, T4&gt; f)\n{\n    return a =&gt; b =&gt; c =&gt; f(c, b, a);\n}\n</code></pre>\n\n<p>Now, we can do something a little more useful:</p>\n\n<pre><code>void UseADifferentlyCurriedFunction()\n{\n    var curryCompare = BackwardsCurry&lt;string, string, bool, int&gt;(String.Compare);\n\n    var caseSensitiveCompare = curryCompare(false);\n    var caseInsensitiveCompare = curryCompare(true);\n\n    var format = Curry&lt;string, string, string, string&gt;(String.Format)("Results of comparing {0} with {1}:");\n\n    var strings = new[] {"Hello", "HELLO", "Greetings", "GREETINGS"};\n\n    foreach (var s in strings)\n    {\n        var caseSensitiveCompareWithS = caseSensitiveCompare(s);\n        var caseInsensitiveCompareWithS = caseInsensitiveCompare(s);\n        var formatWithS = format(s);\n\n        foreach (var t in strings)\n        {\n            Console.WriteLine(formatWithS(t));\n            Console.WriteLine(caseSensitiveCompareWithS(t));\n            Console.WriteLine(caseInsensitiveCompareWithS(t));\n        }\n    }\n}\n</code></pre>\n\n<p>Why are these examples in C#?  Because in F#, function declarations are curried by default.  You don\'t usually need to curry functions; they\'re already curried.  The major exception to this is framework methods and other overloaded functions, which take a tuple containing their multiple arguments.  You therefore might want to curry such functions, and, in fact, I came upon this question when I was looking for a library function that would do this.  I suppose it is missing (if indeed it is) because it\'s pretty trivial to implement:</p>\n\n<pre><code>let curry f a b c = f(a, b, c)\n\n//overload resolution failure: there are two overloads with three arguments.\n//let curryCompare = curry String.Compare\n\n//This one might be more useful; it works because there\'s only one 3-argument overload\nlet backCurry f a b c = f(c, b, a)\nlet intParse = backCurry Int32.Parse\nlet intParseCurrentCultureAnyStyle = intParse CultureInfo.CurrentCulture NumberStyles.Any\nlet myInt = intParseCurrentCultureAnyStyle "23"\nlet myOtherInt = intParseCurrentCultureAnyStyle "42"\n</code></pre>\n\n<p>To get around the failure with String.Compare, since as far as I can tell there\'s no way to specify which 3-argument overload to pick, you can use a non-general solution:</p>\n\n<pre><code>let curryCompare s1 s2 (b:bool) = String.Compare(s1, s2, b)\nlet backwardsCurryCompare (b:bool) s1 s2 = String.Compare(s1, s2, b)\n</code></pre>\n\n<p>I won\'t go into detail about the uses of partial function application in F# because the other answers have covered that already.</p>\n'}]
['https://Stackoverflow.com/questions/8448', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/580/']