F#: Filter items found in one list from another list

后端 未结 3 707
野趣味
野趣味 2021-01-20 06:57

Say I have two lists:

let a = [1 .. 1000]
let b = [250 .. 500]

How do I get a new list that contains the values {1-249, 501-1000}?

3条回答
  •  孤独总比滥情好
    2021-01-20 07:18

    If you want a pure F# solution, your options will vary based on your requirements. If your lists don't contain duplicated items and you don't care about the order of your output, you can just do:

    (Set.of_list a) - (Set.of_list b) |> Set.to_list
    

    If you know that your items are sorted, this should work and be efficient:

    let exclude =
      let rec exclude = function
        | [],_ -> [] 
        | a,[] -> a
        | (x::xs as l),(y::ys as r) -> 
            if x < y then x :: (exclude (xs, r))
            elif x > y then exclude (l, ys)
            else exclude (xs, ys)
      fun a b -> exclude (a,b)
    

    If you have two lists which may contain duplicates, aren't necessarily sorted, you want results in the order they occured in a, and you don't care about performance, you can do:

    a |> List.filter (fun x -> not (List.contains x b))
    

提交回复
热议问题