Currying n参数创建n个函数

时间:2015-03-06 09:34:12

标签: f# functional-programming

我有一个功能

//normal version
let addTwoParameters x y = 
   x + y

转换为咖喱版本,如下所示:

//explicitly curried version
let addTwoParameters x  =      // only one parameter!
   let subFunction y = 
      x + y                    // new function with one param
   subFunction                 // return the subfunction

当我有一个带有4个参数的函数时会怎么样:

let addTwoParameters a b c d = 
       a + b + c + d

currying版本会如何?

1 个答案:

答案 0 :(得分:2)

看起来像这样:

let addTwoParameters a  =
    let subFunction1 b = 
        let subFuction2 c =
            let subFuction3 d =
                a + b + c + d
            subFuction3
        subFuction2
    subFunction1