我有一个 List<List<int>>
在 F# 中。我需要遍历它以寻找给定值 val
.
在 C# 中,我会做类似的事情:
public bool contains(List<List<int>> list, int value)
foreach (l in list ){
foreach(val in l){
if (val == value)
return true; //found value
}
}
return false;
}
我正在寻找 F# 中的等效项。我尝试了以下操作,但我做错了什么,因为我还不习惯 F# 语法:
type foo =
{
l : List<List<float>>
}
let contains (value: float) : bool =
for row in foo.l do
for val in row do
if (val == value)
true
false
上面的代码是错误的。
谁能建议我如何实现该结果?
最佳答案
另一种略有不同的方法
let contains value items =
items
|> Seq.concat
|> Seq.exists ((=) value)
或者,更简洁
let contains value =
Seq.concat >> Seq.exists ((=) value)
关于c# - 遍历 List<List<>> 在 F# 中查找值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8327338/