【发布时间】:2017-11-08 18:36:42
【问题描述】:
给定一个简单的函数,我们对递归调用的结果进行模式匹配,例如:
let rec sumProd = function
| [] -> (0,1)
| x::rest -> let (rSum,rProd) = sumProd rest
(x + rSum,x * rProd)
sumProd [2;5] //Expected (7, 10)
我将如何使用高阶函数将其更改为某些东西,例如foldBack?
let sumProdHigherOrder lst =
List.foldBack (fun x acc -> (acc + x, acc * x)) lst (0,0)
上面的方法看起来差不多,但是调用它会给出错误:The type 'int' does not match the type 'int * int'
sumProdHigherOrder [2;5] //Expected (7, 10)
我错过了什么?
【问题讨论】:
标签: f#