簡體   English   中英

Seq.Length上的F#模式匹配

[英]F# Pattern Match on Seq.Length

我正在學習一些F#並搞亂模式匹配。 我有以下代碼。

Seq.distinct [1; 1; 2] 
   |> match Seq.length with 
      | 1 -> printf "one" 
      | 2 -> printf "two" 
      | _ -> printf "other"

但是在運行或嘗試編譯時會出現以下錯誤:

This expression was expected to have type
'a -> int 
but here has type
int

我不太確定問題是什么,究竟是什么問題。 我確定我錯過了一些簡單的東西,但是我還有另一種方法嗎?

你可以這樣做:

match Seq.distinct [1; 1; 2] |> Seq.length with 
| 1 -> printf "one" 
| 2 -> printf "two" 
| _ -> printf "other"

或這個:

Seq.distinct [1; 1; 2] 
|> Seq.length
|> function
    | 1 -> printf "one" 
    | 2 -> printf "two" 
    | _ -> printf "other"

但是,就像你想要的那樣,你將Seq.distinct的輸出Seq.distinctmatch表達式而不是Seq.length

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM