玩弄F#,我很困惑以下行爲。當List.reduce (>>)
被註釋掉,有來自http://fsharpforfunandprofit.com/posts/conciseness-functions-as-building-blocks/歸結在這個例子中,錯誤爲什麼在這個新手示例中需要List.reduce?
defaultLabel |> showRainbow
----------------^^^^^^^^^^^
This expression was expected to have type
CoolLabel -> 'a
but here has type
(CoolLabel -> CoolLabel) list
:
// 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。」
謝謝。