Understanding currying of List.fold_right


#1

I’m learning about List.fold_right.

I have this simple function that appends two lists:

let append = (xs, ys) => List.fold_right((x, acc) => [x, ...acc], xs, ys);

This compiles successfully.

The function arguments match the last two arguments of fold_right, so I tried changing to this:

let append2 = List.fold_right((x, acc) => [x, ...acc]);

This produces an error:

This expression's type contains type variables that can't be generalized:
  (list('_weak1), list('_weak1)) => list('_weak1)

I’m curious why the first version was able to compile successfully, but not the second?

I did notice that if I use the function then the error goes away:

append2([1], [2]);

However if I also try use it with another type:

append2(["a"], ["b"]);

It produces a compile error:

This has type:
  string
But somewhere wanted:
  int

Thanks!


#2

There’s a great explanation of this here: https://ocamlverse.github.io/content/weak_type_variables.html


#3

Great, thanks @yawaramin