Cod*_*Guy 4 f# functional-programming
我的问题是关于 F# 中的列表过滤。是否有一个内置函数允许过滤列表,仅返回不满足条件的列表?
let listOfList = [ [1;2;3;4;5]; [6;7;8;9;10]; [11;2;5;14;1] ]
let neededValue = 1
Run Code Online (Sandbox Code Playgroud)
我知道 F# 具有 List.Contains() 但我只想返回不满足条件的列表。
let sortedLists = listOfList |> List.filter(fun x -> x <> x.Contains(neededValue)
Run Code Online (Sandbox Code Playgroud)
这显然不起作用,因为在这种情况下,我将列表与列表是否包含特定值进行比较。我该怎么做?在这种情况下我想要的输出是:
sortedLists = [ [6;7;8;9;10] ]
Run Code Online (Sandbox Code Playgroud)
你们离得太近了!改为x <>,not <|就可以了。
let listOfList = [ [1;2;3;4;5]; [6;7;8;9;10]; [11;2;5;14;1] ]
let neededValue = 1
let sortedLists = listOfList |> List.filter(fun x -> not <| x.Contains(neededValue))
Run Code Online (Sandbox Code Playgroud)
该not函数允许您对布尔值取反,以便过滤表达式中的类型匹配。