Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One effective use of curried functions is decreasing of amount of code.</p> <p>Consider three functions, two of which are almost identical:</p> <pre><code>(define (add a b) (action + a b)) (define (mul a b) (action * a b)) (define (action kind a b) (kind a b)) </code></pre> <p>If your code invokes <code>add</code>, it in turn calls <code>action</code> with kind <code>+</code>. The same with <code>mul</code>.</p> <p>You defined these functions like you would do in many imperative popular languages available (some of them have been including lambdas, currying and other features usually found in functional world, because all of it is terribly handy).</p> <p>All <code>add</code> and <code>sum</code> do, is wrapping the call to <code>action</code> with the appropriate <code>kind</code>. Now, consider curried definitions of these functions:</p> <pre><code>(define add-curried ((curry action) +)) (define mul-curried ((curry action) *)) </code></pre> <p>They've become considerable shorter. We just curried the function <code>action</code> by passing it only one argument, the <code>kind</code>, and got the curried function which accepts the rest two arguments.</p> <p>This approach allows you to write less code, with high level of maintainability.</p> <p>Just imagine that function <code>action</code> would immediately be rewritten to accept 3 more arguments. Without currying you would have to rewrite your implementations of <code>add</code> and <code>mul</code>:</p> <pre><code>(define (action kind a b c d e) (kind a b c d e)) (define (add a b c d e) (action + a b c d e)) (define (mul a b c d e) (action * a b c d e)) </code></pre> <p>But currying saved you from that nasty and error-prone work; you don't have to rewrite even a symbol in the functions <code>add-curried</code> and <code>mul-curried</code> at all, because the calling function would provide the necessary amount of arguments passed to <code>action</code>.</p>
    singulars
    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. 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