let rec setintersect list list =
is wrong: the two arguments should be named differently (you should of course update the call to setintersect2
accordingly), otherwise the second will shadow the first. I would have thought that OCaml would have at least warned you about this fact, but it appears that it is not the case.
Apart from that, the code seems to do the trick. There are a couple of things that could be improved though:
setintersect
itself is not recursive (onlysetintersect2
is), you thus don't need therec
- you should find a different name for the argument of
setintersect2
. In particular, it is not obvious which is the accumulator (acc
oraccu
will be understood by most OCaml programmers in these circumstances). c@[h1]
is inefficient: you will traversec
completely each time you append an element. It's better to doh1::c
and reverse the result at the end- As a bonus point, if you append element at the beginning of
c
, and assume thata
is ordered, you don't have to callsetsimplify
at the end of the call: just check whetherc
is empty, and if this is not the case, appendh1
only if it is not equal to the head ofc
.