I am writing a function to remove neighbors from a list which have the same value. I don't understand what the syntax error is here. This is what I have:
let rec rem_dup_neighb l =
let rec rem_dup_neighb_aux l lastseen retl =
match l with
[]->retl
|[()]->[()]
| (y::rest) -> if(y==lastseen) then rem_dup_neighb_aux l lastseen retl else rem_dup_neighb_aux l y (y::retl)
in rem_dup_neighb_aux l 9000 [];;
I get the following error for the last line of the function:
Error: This expression has type int but an expression was expected of type
unit
As an example, if you pass in [1;2;2;3;5] to the function, it should return [1;2;3;5]
Any help is appreciated. Thanks
UPDATE: Function seems to be infinite looping:
let rec rem_dup_neighb l =
let rec rem_dup_neighb_aux l lastseen retl =
match l with []->retl
| (y::rest) -> if(y==lastseen) then rem_dup_neighb_aux l lastseen retl else rem_dup_neighb_aux l y (y::retl)
in
match l with
(lastseen::rest) -> rem_dup_neighb_aux l lastseen []
UPDATE 2: wasn't reducing the problem per iteration. Function seems to be returning [5;3;2] instead of [1;2;3;5] now though.
let rec rem_dup_neighb l =
let rec rem_dup_neighb_aux l lastseen retl =
match l with []->retl
| (y::rest) -> if(y==lastseen) then rem_dup_neighb_aux rest lastseen retl else rem_dup_neighb_aux rest y (y::retl)
in
match l with
[]->[]
|(lastseen::rest) -> rem_dup_neighb_aux l lastseen []
UPDATE 3:
let rec rem_dup_neighb l =
let rec rem_dup_neighb_aux l lastseen retl =
match l with []->retl
| (y::rest) -> if(y==lastseen) then rem_dup_neighb_aux rest lastseen retl else rem_dup_neighb_aux rest y (y::retl)
in
match l with
[]->[]
|(lastseen::rest) -> rem_dup_neighb_aux l lastseen [lastseen]
rem_dup_neighb_auxeach time. You want the to proceed along the list with each recursive call, so passrestthere.List.revshould suffice for that. (Accumulating a list and then reversing it at the end is quite a common pattern in functional programming.)