Home > other >  Generic type not infered in list operations OCaml
Generic type not infered in list operations OCaml

Time:06-07

While trying to find a certain sequence in a list I came up with the following:

type 'a sequence = {
  pos : int;
  mutable count : int;
  seq : 'a list;
}

let rec all_seq_for seq len pos = 
  match seq with
  | [] -> []
  | _::xs -> 
    let length = List.length seq in
    if length > len then 
      List.append {pos = pos; count = 1; seq = (sublist seq 0 (len - 1))} (all_seq_for xs len (pos  1)) 
    else if length = len then 
      seq 
    else
      []

the line then List.append {pos = pos... throws the Error: This expression has type 'a but an expression was expected of type 'b list I don't understand why the type of 'a sequence list is not automatically inferred. How can this error be solved?

CodePudding user response:

The actual error says this:

Error: This expression has type 'a sequence
       but an expression was expected of type 'b list

which is a crucial difference.

You are supplying a record as the first argument of List.append, but the first argument should be a list. Possibly you should just enclose your expression { ... } in square brackets: [ { ... } ].

Or maybe the error is deeper than that; I thought 'a sequence was just a description of what you're looking for. If so, it doesn't make sense to append it to a list (it seems to me).

  • Related