I want to do 2 things after the expression "then" in the expression "if .. then .. else"

let rec filtersList2fromList1 (List1:string list) (List2:string list) : string list =  
 let finalList = [] in  
 match List1 with  
 | s :: tl -> if List.mem s List2 = true   
 then finalList @ [s] else filtersList2fromList1 tl List2  
        | [] -> []

so that

filtersList2fromList1 ["x";"y";"z"] ["z";"x"] would be ["x";"z"]  
filtersList2fromList1 ["x";"y";"z"] ["x"] would be ["x"]

what I would like to add if the statement “if” is true, not only will it execute “finalList @ [s]”, but also “filtersList2fromList1 tl List2” so that it is recursive. Without doing "filtersList2fromList1 tl List2" when it's true,

filtersList2fromList1 ["x";"y";"z"] ["z";"x"]it will only be ["x"]that which is wrong.

How do I solve this problem?

Many thanks

+5
source share
2 answers

To answer your specific question, you should use either a comma construct or a construct let...in. In your case, none of them will do what you want.

, List , , , :

let filterList2fromList1 list1 list2 =
  List.filter (fun x -> List.mem x list2) list1
+6

, , , , , dolls_of, filtersList2fromList1. , List1 List2 List1 List2, .

, @ O(n), . , , finalList , @.

: , ;. dolls_of - , - .

, , :

if List.mem s list2   
then s :: filtersList2fromList1 tl list2
else filtersList2fromList1 tl list2
+4

All Articles