简体   繁体   English

Seq.Length上的F#模式匹配

[英]F# Pattern Match on Seq.Length

I'm learning some F# and messing around with pattern matching. 我正在学习一些F#并搞乱模式匹配。 I have the below code. 我有以下代码。

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

But when running or trying to compile it gives the error of: 但是在运行或尝试编译时会出现以下错误:

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

I'm not quite sure what the issue is and what exactly is being asked. 我不太确定问题是什么,究竟是什么问题。 I'm sure I'm missing something simple, but is there another way I should be doing this? 我确定我错过了一些简单的东西,但是我还有另一种方法吗?

You could do either this: 你可以这样做:

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

or this: 或这个:

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

But, as is, you're piping the output from Seq.distinct to a match expression and not Seq.length as you intend. 但是,就像你想要的那样,你将Seq.distinct的输出Seq.distinctmatch表达式而不是Seq.length

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM