简体   繁体   中英

Filtering two lists f#

I have two string lists, one with humanIds that fulfill some conditions (humanosPosibles), and another list that contain humanIds that may or may not fulfill the conditions (likesTodo).The thing is, I need to create one list that contains only the humansIds that are in likesTodo and also in humanosPosibles. I tried doing it with recursion and a list.filter. I'll put my code for you to see what is it that I am doing wrong. If you think this is not the best way to do it, please make suggestions.

  let rec pasa (likesTodo:string list) (humanosPosibles:string list) x y =
        if (y < humanosPosibles.Length) then
            if (likesTodo.[x] = humanosPosibles.[y]) then
                true 
            else
                pasa likesTodo humanosPosibles x (y+1)  
        else
            false  

 let genteFiltrada = List.filter (fun x -> pasa likesTodo humanosPosibles 0 0 ) likesTodo

genteFiltrada corresponds to the list that I need to create. The last time I forgot to say that if one item of likesTodo is duplicated I need it to be two times in the new list. Therefore, set won't work.

Here is a solution using set, as what you want is the intersection:

let intersectlists a b = 
    let la,lb = Set.ofList a, Set.ofList b
    Set.intersect la lb |> Set.toList

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM