玩弄 F#,我对以下行为感到困惑。当List.reduce (>>)
被注释掉时,有错误
defaultLabel |> showRainbow
----------------^^^^^^^^^^^
This expression was expected to have type
CoolLabel -> 'a
but here has type
(CoolLabel -> CoolLabel) list
在这个例子中归结为http://fsharpforfunandprofit.com/posts/conciseness-functions-as-building-blocks/:
// create an underlying type
type CoolLabel = {
label : string;
}
let defaultLabel =
{label="";}
let setLabel msg label =
{label with CoolLabel.label = msg}
let rainbow =
["red";"orange";"yellow";"green";"blue";"indigo";"violet"]
let showRainbow =
rainbow
|> List.map setLabel
|> List.reduce (>>)
// test the showRainbow function
defaultLabel |> showRainbow
当List.reduce (>>)
被删除时,我认为 showRainbow 应该返回一个 CoolLabel 列表,编译器会很酷。
编辑->(忽略这句话,因为下面的答案改变了我的理解。):“顺便说一句,我知道 List.reduce (>>) 将返回列表中的最后一个 CoolLabel。”
谢谢。