Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I think the most important application of currying is those languages in which functions can take only a few arguments. Belive it or not, there are lots of such programming languages. It all started with mathematical constructs.

The lambda calculus is a very simple model of computation where every object is a function with exactly one argument and one return value – so the argument and the return value are functions like this as well. It has very few building blocks and still do any calculation. Furthermore, there are variants of this which do not use lambda expressions or variables and as such can be seen simpler: the SKI calculus (which is the base of the Unlambda obfuscated programming language), and the more practical FP. (Btw, these models are also side-effectless becuase mathematicians always had an affection for that, but that's not really important in this story.)

In such languages, it is still possible to simulate functions with arbitarily many arguments in two ways: one is currying, and the other is that you call a function with a single aggreate object (tuple, record, list or whatever you have in the language) containing all the arguments to the function.

However, the idea of limiting functions to only a few arguments was used in the Real World as well. Functional programming languages in the large family1 of ML (Standard ML, Caml, Haskell) have functions that accept only one argument. These languages support both currying and passing tuples by giving easy syntax to calling a curried function, creating tuples, defining a curried function, and defining a function that deconstructs a tuple argument. Indeed, function calling in these languages is denoted by juxtaposition and associates to the direction for calling a curried function, so you can just write f x y instead of f(x)(y), and function definitions can be curried rightaway so you can say fn x y => z instead of fn x => fn y => z etc. (Standard ML still seems to be supporting tuples more than currying because datatype constructor can not be curried.)

The J programming language is a bit different. In it, functions can have only one or two arguments. This makes it much easyer to define functions without explicitly naming the arguments (this is apparently practiced by haskell progammers as well, and is called pointfree style). On the other hand, even though J also has explicit function definitions, and some syntax ease about curried functions, it doesn't give as much helper syntax for multivariate functions as the ML languages do.

Not relevant here are those languages that limit the number of arguments to functions for some technical reason only, not by principle, and typically not as low as above. For example, the DOS shell allows access to only nine command-line arguments (the rest can still be accessed with the shift command); regular expressions in sed and joe have capturing parenthesis but you can only refer to the first 9 or 10 captures resp; some low-level cpu calling conventions limit how many arguments you can pass to a system call or such wierd functions.

1 Take care. As this is a rather large family, rather like the family of lisp-like languages, so some of its members might be offended if you count them in.

Update: link to the J programming language.


In reply to Re: Currying--useful examples? by ambrus
in thread Currying--useful examples? by macrobat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-16 20:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found