News for hackers. Stuff that matters.
Re:What is the advantage of currying? by jacquesm on Saturday February 02, 2013 @08:04AM (#5155887)
I read all those answers and I'm either not getting it or there really is something missing there. Isn't the big advantage that when you use currying that all functions have an arity of '1'?

That means no more messing with varargs, named args (on the calling side), positional args, defaults and so on, every function takes one argument and one argument only, currying is then used to bypass the limitations of having only one argument to work with.

Re:What is the advantage of currying? by jerf on Saturday February 02, 2013 @10:23AM (#5156154)
There are some things in the world of programming or software engineering that are difficult to explain the cost/benefits of in a nice little blog post, because the costs or benefits don't become apparent in anything that fits in a blog post.

As you'd suspect from that lead-in, currying is one of those things. Explaining how it works is easy in a blog post, explaining why it is interesting is hard to do without being A: trivial or B: gibberish if you don't already know Haskell fairly well. Both approaches are taken in the StackOverflow there.

While it does mean you lose default and named args, it turns out to hurt less than you'd think, because the whole runtime and library is set up around functions without named args. I'm not sure exactly why, but when you can break things down into such smaller grained functions it seems to matter less. Positional is handled by a variety of functions that can reorder arguments, most commonly "flip" which switches the order of the first and second arguments, and while that is used and idiomatic it's probably used less than you'd think.

I can assure you it gets used a lot in idiomatic Haskell.

Re:What is the advantage of currying? by rwmj on Saturday February 02, 2013 @09:04AM (#5155966)
In a traditionally compiled language, currying doesn't provide varargs: the compiler still has to know how many parameters a function takes before it can be called. You cannot write `printf "%d" 1' expecting that at runtime printf is going to parse the string and read further arguments, because the compiler wouldn't know when printf can be called.

OCaml famously (or notoriously) hacks this knowledge into the compiler, so the compiler knows about format strings and parses them. This is nicely typesafe, but means you cannot use varargs in your own functions, and there are odd restrictions on the format string. (You can use the camlp4 preprocessor, or lists-as-parameters, to get around this).

Re:What is the advantage of currying? by chongli on Saturday February 02, 2013 @11:36AM (#5156411)
Haskell has printf without using lists-as-parameters. It accomplishes this by doing some very clever type-level trickery. The key idea is in the recursive definition of this instance of PrintfType:

    instance (PrintfArg a, PrintfType r) => PrintfType (a -> r) where
Re:What is the advantage of currying? by philsnow on Saturday February 02, 2013 @03:43PM (#5157263)
Your snippet got cut off, care to paste the rest? I'm guessing it works by popping the format string and the first operand off the args, and then finding the first formatting escape in the format string and formatting it, then recurse.
Re:What is the advantage of currying? by chongli on Saturday February 02, 2013 @04:52PM (#5157504)
Yeah, sorry. Here's the full snippet:

    instance (PrintfArg a, PrintfType r) => PrintfType (a -> r) where
    spr fmts args = \ a -> spr fmts (toUPrintf a : args)
And spr is a polymorphic function of the type:

    spr :: String -> [UPrintf] -> t
Essentially the way this works is that it recursively conses up a list of the arguments, calling toUPrintf on each.
Re:What is the advantage of currying? by guard-of-terra on Saturday February 02, 2013 @09:07AM (#5155977)
"the compiler still has to know how many parameters a function takes before it can be called"

I guess it's better viewed as: type system has to figure out whether a consecutive currying/calling will return a scalar/function of zero arguments.

Re:What is the advantage of currying? by pcwalton on Saturday February 02, 2013 @09:29AM (#5156023)
Defaults and named args don't really work that well with currying. OCaml ended up adding them as a separate feature (so functions are now sorta-curried, sorta-not in OCaml).

Not that currying is a bad thing, of course. For the right functional combinators (map, reduce, etc.) currying can be put to very elegant use.

Re:What is the advantage of currying? by Peaker on Saturday February 02, 2013 @08:18AM (#5155911)
The lack of named args is seen as a disadvantage. Varargs are modeled with Maybe params and such.

The main benefit is syntactic ease of partial application.