F# 元组模式匹配问题
有没有一种方法可以在 F# 中进行模式匹配,以便检查双元素元组中的两个元素是否相等。我对 F# 非常陌生,对模式匹配的整个概念也很陌生。我在想一些类似的事情......
let rec funct = function
|(xs, xs) -> 0
etc. etc. etc.
但我想那太简单了哈哈。有什么建议吗?或者有没有办法做到这一点?感谢您的帮助,你们非常有帮助!
Is there a way to pattern match in F# so that it will check to see if both elements in a two element tuples are equal. I'm incredibly new to F#, and also new to the whole concept of Pattern Matching in general. I was thinking something along the lines of...
let rec funct = function
|(xs, xs) -> 0
etc. etc. etc.
But I guess that would be too easy hah. Any suggestions? Or is there even a way to do that? Thanks for your help, you guys are incredibly helpful!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
http://msdn.microsoft.com/en-us/library/dd233242.aspx
http://msdn.microsoft.com/en-us/library /dd547125.aspx
http://msdn.microsoft.com/en-us/library/dd233242.aspx
http://msdn.microsoft.com/en-us/library/dd547125.aspx
Brian的答案是正确的,但既然你还询问了其他方法,我将添加一个替代方案 - 我通常不使用
match
来处理元组,因为简单的分解总是会成功(除非你使用一些复杂的方法)模式或何时
)。在编写函数时,我可能会写:(但是当然,这取决于上下文 -
if
可能适合小函数)Brian's answer is correct, but since you also asked about other ways I'll add an alternative - I don't usually use
match
to work with tuples because simple decomposition will always succeed (unless you use some complicated patterns orwhen
). When writing a function, I would likely write:(But of course, it depends on the context - the
if
is probably good for small functions)