函数名称:交集:取2个列表并返回出现在所有元素中的列表Ocaml:递归:交集
ie:[1; 2; 2; 3; 4; 4; 3] [2; 3] - > [2; 2; 3; 3]
let rec intersection (l1: int list) (l2: int list) : int list =
begin match l1, l2 with
| hd :: tl, hd2 :: tl2 -> if hd = hd2 then hd :: intersection tl l2
else intersection tl l2
| _ -> []
end
没有与此代码的问题,但我不知道如何解决它 - 该代码将运行通过,并得到[2; 2],因为它始终与l2中的第一个元素相比,但是我想让l1也与tl2比较,有没有人有任何建议?
Should I add another match [], [] -> to clarify base case at the beginning?
*你会怎样写它?你面临什么问题?请向我们展示您的尝试或告诉我们您的方法。 StackOverflow可以帮助你做家庭作业,但我们不会为你解决它。 – Bergi
有没有更有效的方法来实现这个代码? 每隔一个:每个第二个元素 ie:[1; 2; 3; 4; 5] - > [1; 3; 5] let rec every_other(l:int list):int list = begin match l with | [] - > [] | hd :: tl - > hd :: every_other tl end – anonymoususer
或者这个? let rec all_even(l:int list):bool =开始匹配l with | [] - > true | hd :: tl - >(hd mod 2 = 0)&& all_even tl end – anonymoususer