Practical use of curried functions?

后端 未结 10 825
粉色の甜心
粉色の甜心 2020-12-14 07:01

There are tons of tutorials on how to curry functions, and as many questions here at stackoverflow. However, after reading The Little Schemer, several books, tutorials, blog

相关标签:
10条回答
  • 2020-12-14 07:35

    One effective use of curried functions is decreasing of amount of code.

    Consider three functions, two of which are almost identical:

    (define (add a b)
      (action + a b))
    
    (define (mul a b)
      (action * a b))
    
    (define (action kind a b)
      (kind a b))
    

    If your code invokes add, it in turn calls action with kind +. The same with mul.

    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).

    All add and sum do, is wrapping the call to action with the appropriate kind. Now, consider curried definitions of these functions:

    (define add-curried
      ((curry action) +))
    
    (define mul-curried
      ((curry action) *))
    

    They've become considerable shorter. We just curried the function action by passing it only one argument, the kind, and got the curried function which accepts the rest two arguments.

    This approach allows you to write less code, with high level of maintainability.

    Just imagine that function action would immediately be rewritten to accept 3 more arguments. Without currying you would have to rewrite your implementations of add and mul:

    (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))
    

    But currying saved you from that nasty and error-prone work; you don't have to rewrite even a symbol in the functions add-curried and mul-curried at all, because the calling function would provide the necessary amount of arguments passed to action.

    0 讨论(0)
  • 2020-12-14 07:39

    We cannot directly compose functions that takes multiple parameters. Since function composition is one of the key concept in functional programming. By using Currying technique we can compose functions that takes multiple parameters.

    0 讨论(0)
  • 2020-12-14 07:43

    So you don't have to increase boilerplate with a little lambda.

    0 讨论(0)
  • 2020-12-14 07:43

    In and of itself currying is syntactic sugar. Syntactic sugar is all about what you want to make easy. C for example wants to make instructions that are "cheap" in assembly language like incrementing, easy and so they have syntactic sugar for incrementation, the ++ notation.

     t = x + y
     x = x + 1
    

    is replaced by t = x++ + y

    Functional languages could just as easily have stuff like.

    f(x,y,z) = abc 
    g(r,s)(z) = f(r,s,z). 
    h(r)(s)(z) = f(r,s,z)
    

    but instead its all automatic. And that allows for a g bound by a particular r0, s0 (i.e. specific values) to be passed as a one variable function.

    Take for example perl's sort function which takes sort sub list where sub is a function of two variables that evaluates to a boolean and list is an arbitrary list.

    You would naturally want to use comparison operators (<=>) in Perl and have sortordinal = sort (<=>) where sortordinal works on lists. To do this you would sort to be a curried function.
    And in fact sort of a list is defined in precisely this way in Perl.

    In short: currying is sugar to make first class functions more natural.

    0 讨论(0)
提交回复
热议问题