recursionocaml

Working with option and list in OCaml


I am having problems with recursive functions that takes a list and return an option list. For example a function all_except_one:

val all_except_one : 'a -> 'a list -> 'a list option = <fun> 

Where the first occurrence of 'a is removed from the list. If the 'a is not in the list you should return None.

without the option I have code that looks like this:

let same_string s1 s2 =
  s1 = s2

let rec all_except_one str str_l =
  match str_l with
  | [] -> []
  | hd::tl -> if same_string hd str
              then tl
              else hd::(all_except_one str tl)

but whenever I try and add the option, it gets in the way when doing my recursive call.


Solution

  • An alternative to matching on the result of a recursive call is to write a helper function with an accumulator argument:

    let remove_first elt list =
      let rec loop acc = function
        | [] -> None
        | x::xs ->
            if x = elt then Some (List.rev_append acc xs)
            else loop (x::acc) xs in
      loop [] list
    

    A minor advantage of doing it this way is that the loop becomes tail recursive.