我无法确定在执行转换的函数上运行聚合器所需的语法。
我有以下代码:
type Suit = | Spades| Clubs | Diamonds | Hearts
type Face = | Two | Three | Four | Five
| Six | Seven | Eight | Nine | Ten
| Jack | Queen | King | Ace
type Card = {Face:Face; Suit:Suit}
我无法编译这个函数:
let getCount (hand:Card list) =
let getFaceValue face =
match face with
| Two -> 2
| Three -> 3
| Four -> 4
| Five -> 5
| Six -> 6
| Seven -> 7
| Eight -> 8
| Nine -> 9
| Ten -> 10
| Jack -> 10
| Queen -> 10
| King -> 10
| Ace -> 11
let sumOfHand = hand |> List.sum (fun c -> getFaceValue c.Face)
sumOfHand
期望一个支持运算符'+‘的类型,但给定一个函数类型。您可能缺少一个函数的参数。
为什么我会得到这个错误,如何使用聚合函数中的转换呢?
发布于 2015-12-27 13:45:55
List.sum
只接受一个参数(一个列表),并使用+
运算符计算所有元素的和。
若要通过应用于列表中每个元素的函数求和,请使用List.sumBy
let sumOfHand = hand |> List.sumBy (fun c -> getFaceValue c.Face)
发布于 2015-12-27 13:36:49
你应该先映射然后和
hand |> List.map (fun x ...) |> List.sum
https://stackoverflow.com/questions/34480937
复制相似问题