私の Ocaml プロジェクトには、要素を複製せずにリストを別のリストに追加するのに役立つヘルプ機能があります。たとえば、に追加list x: [d, e, f, g]
するとlist y [a, b, c, d]
、結果は [a、b、c、d、e、f、g] になります。
私が書いた関数は次のようなものです:
(* helper function checks if list contains element *)
let rec find e l =
match l with
[] -> false
|(h::t) -> if (h = e) then true else find e t
;;
(* helper function append l1 to l2 without duplicate *)
let rec help_append_list l1 l2 =
match l1 with
[] -> l2
|(h::t) -> if (find h l2 = false) then (help_append_list t ([h]@l2)) else (help_append_list t l2)
;;
しかし、これを使用するとうまく機能していないように見えますが、まだ重複した要素が表示されていることがわかりました。
上記の関数を見て、それらを修正する方法についていくつか提案してください...
ありがとう=)