F# 是否具有判断列表是否包含特定值的函数?
Does F# has a function to tell if a list contains a specific value?
Haskell 有 "elem" 谓词告诉像:
Prelude> 5 `elem` [2,4..10]
False
在F#中,如何方便的判断一个值是在list、array、seq、map、dictionary中?
您可以使用:
List.exists ((=) 5) [1..5]
或者按照其他答案中的建议,如果您有最新的 F# 版本,直接 List.contains
。
相同的功能可用于 Seq
。
在 F# 中是
List.contains <element> <list>
示例:
List.contains 5 [2..2..10]
-->
val it : bool = false
contains
也为其他容器类型定义。
Haskell 有 "elem" 谓词告诉像:
Prelude> 5 `elem` [2,4..10]
False
在F#中,如何方便的判断一个值是在list、array、seq、map、dictionary中?
您可以使用:
List.exists ((=) 5) [1..5]
或者按照其他答案中的建议,如果您有最新的 F# 版本,直接 List.contains
。
相同的功能可用于 Seq
。
在 F# 中是
List.contains <element> <list>
示例:
List.contains 5 [2..2..10]
-->
val it : bool = false
contains
也为其他容器类型定义。