Anatoliy Sokolov
Anatoliy Sokolov

Reputation: 397

Have Trouble Understanding OCaml Code

I need to modify an OCaml function:

let removeDuplicates l = 
  let rec helper (seen,rest) = 
      match rest with 
        [] -> seen
      | h::t -> 
        let seen' = failwith "to be written" in
        let rest' = failwith "to be written" in 
      helper (seen',rest') 
  in
      List.rev (helper ([],l));;

The function needs to take a list l and return the list with all duplicates removed. The failwith "to be written" parts is where I'm supposed to write my code. I understand how the helper function works but am having trouble understanding this part helper (seen',rest'). I'm not exactly sure how the function is supposed to flow with this part or how it works when you include a bunch of in's all together. We are allowed to use List.rev which reverses a list and list.mem which returns true if a certain element is in a list. Can someone please explain to me how the flow of the function is supposed to work so I can start to write a solution.

Upvotes: 0

Views: 91

Answers (1)

Jeffrey Scofield
Jeffrey Scofield

Reputation: 66823

That line is confusing because it's indented incorrectly, or so I would claim. The proper indentation looks like this:

    let seen' = failwith "to be written" in
    let rest' = failwith "to be written" in 
    helper (seen',rest') 

What it's saying is: calculate a new value for seen and a new value for rest, then call yourself recursively with the two new values.

Upvotes: 1

Related Questions