Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For languages that support currying and partial-application easily, there is one compelling series of arguments, originally from Chris Okasaki:</p> <ul> <li><strong>Put the data structure as the last argument</strong></li> </ul> <p>Why? You can then <a href="http://www.haskell.org/pipermail/libraries/2005-August/004297.html" rel="noreferrer">compose operations on the data</a> nicely. E.g. <code>insert 1 $ insert 2 $ insert 3 $ s</code>. This also helps for <a href="http://www.haskell.org/pipermail/libraries/2005-August/004323.html" rel="noreferrer">functions on state</a>.</p> <p>Standard libraries such as "containers" <a href="http://hackage.haskell.org/packages/archive/containers/0.4.0.0/doc/html/Data-Map.html#g:5" rel="noreferrer">follow this convention</a>.</p> <p>Alternate arguments are sometimes given to put the data structure first, so it can be closed over, yielding functions on a static structure (e.g. lookup) that are a bit more concise. However, the broad consensus seems to be that this is less of a win, especially since it pushes you towards heavily parenthesized code.</p> <ul> <li><strong>Put the most varying argument last</strong></li> </ul> <p>For recursive functions, it is common to put the argument that varies the most (e.g. an accumulator) as the last argument, while the argument that varies the least (e.g. a function argument) at the start. This composes well with the data structure last style.</p> <hr> <p>A summary of the Okasaki view is given in <a href="http://hackage.haskell.org/packages/archive/EdisonAPI/1.2.1/doc/html/Data-Edison.html" rel="noreferrer">his Edison library</a> (again, another data structure library):</p> <ul> <li><strong>Partial application</strong>: arguments more likely to be static usually appear before other arguments in order to facilitate partial application.</li> <li><strong>Collection appears last</strong>: in all cases where an operation queries a single collection or modifies an existing collection, the collection argument will appear last. This is something of a de facto standard for Haskell datastructure libraries and lends a degree of consistency to the API.</li> <li><strong>Most usual order</strong>: where an operation represents a well-known mathematical function on more than one datastructure, the arguments are chosen to match the most usual argument order for the function.</li> </ul>
 

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