Search code examples
ocamltypechecking

ocaml type missmatch unit vs list


For this signature

val chooser: string list * string list -> string list

and this implementation

 let rec chooser (inputList, trueList) = match inputList with
      [] -> []
    | iH::iT -> if (List.hd trueList)="True" 
        then iH::(chooser iT List.tl trueList)

I am getting the following error:

Error: This variant expression is expected to have type unit The constructor :: does not belong to unit

What am I doing wrong?


Solution

  • The result of if ... then with no else has to be unit, because the value will be () (the value of type unit) when the expression is false.

    In other words, you need an else part for your if to get the type you want. What should the value be when the comparison is false?