Jon*_*Jon 2 f# pattern-matching
我正在学习一些F#并搞乱模式匹配.我有以下代码.
Seq.distinct [1; 1; 2]
|> match Seq.length with
| 1 -> printf "one"
| 2 -> printf "two"
| _ -> printf "other"
Run Code Online (Sandbox Code Playgroud)
但是在运行或尝试编译时会出现以下错误:
This expression was expected to have type
'a -> int
but here has type
int
Run Code Online (Sandbox Code Playgroud)
我不太确定问题是什么,究竟是什么问题.我确定我错过了一些简单的东西,但是我还有另一种方法吗?
你可以这样做:
match Seq.distinct [1; 1; 2] |> Seq.length with
| 1 -> printf "one"
| 2 -> printf "two"
| _ -> printf "other"
Run Code Online (Sandbox Code Playgroud)
或这个:
Seq.distinct [1; 1; 2]
|> Seq.length
|> function
| 1 -> printf "one"
| 2 -> printf "two"
| _ -> printf "other"
Run Code Online (Sandbox Code Playgroud)
但是,按原样,您将输出汇总Seq.distinct到match表达式而不是Seq.length您想要的.