Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(F#) Inbuilt function to filter a list if it does not contain a specific value

My question is regarding list filtering in F#. Is there a built in function that allows the filtering of lists where it only returns those that do not satisfy a condition?

let listOfList = [ [1;2;3;4;5]; [6;7;8;9;10]; [11;2;5;14;1] ]
let neededValue = 1

I know that F# features List.Contains() however I want to only return lists which do not satisfy the condition.

let sortedLists = listOfList |> List.filter(fun x -> x <> x.Contains(neededValue)

This obviously does not work because in this instance I'm comparing a list to whether a list contains a specific value. How would I do this? My desired output in this instance would be:

sortedLists = [ [6;7;8;9;10] ]
like image 678
Code Guy Avatar asked Oct 27 '25 19:10

Code Guy


1 Answers

You were so close! Change x <> to not <|, and it will work.

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))

The not function allows you to negate a boolean value, so that the types in the filter expression match up.

like image 85
Samantha Avatar answered Oct 31 '25 04:10

Samantha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!