I am working on a project with OCaml and there are some problems regarding to arrays that I am not sure with. I am not allowed to use the List module, so please give me some idea or suggestion with my works.
First, I already implemented a function 'a list -> 'a list called uniq that return a list of the uniq elements in an array, for example uniq [5;6;5;4] => [6;5;4]
Here is my implementation:
let rec uniq x =
let rec uniq_help l n =
match l with
[] -> []
| h :: t -> uniq_help t, n if (n = h) else (h :: (uniq_help(t, n)))
match x with
[] -> []
| h::t -> uniq_help t, h
;;
I mot sure this is a correct implementation, can someone give me some suggestion or correctness?
You functions are syntactically incorrect for various reasons:
uniq_helptakes two elements so you have to invoke it usinguniq_help t n, notuniq_help(t, n)and the like.if/elseexpression should have the form ofif cond then expr1 else expr2.uniq_helplocally inuniq, you need aninkeyword.After fixing syntax errors, your function looks like:
However, to be sure that each element is unique in the list, you have to check uniqueness for all of its elements. One quick fix could be: