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.
filtersList2fromList1 ["x";"y";"z"] ["z";"x"]
["x"]
How do I solve this problem?
Many thanks
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.
let...in
, List , , , :
List
let filterList2fromList1 list1 list2 = List.filter (fun x -> List.mem x list2) list1
, , , , , dolls_of, filtersList2fromList1. , List1 List2 List1 List2, .
dolls_of
filtersList2fromList1
List1
List2
, @ O(n), . , , finalList , @.
@
O(n)
finalList
: , ;. dolls_of - , - .
;
, , :
if List.mem s list2 then s :: filtersList2fromList1 tl list2 else filtersList2fromList1 tl list2