Thanks for cleaning up your question some, @otaku_for_ever.
If I enter your declarations at the top-level one at a time, I get an error at the first one:
# let min_list2 lst =
let a = List.nth lst in
let b = List.hd lst in
List.fold_left min a b;;
Line 4, characters 25-26:
Error: This expression has type 'a but an expression was expected of type
(int -> 'a) list
The type variable 'a occurs inside (int -> 'a) list
Note that this provides critical information that you’ve omitted in your question. First, it tells us which declaration is having trouble (the first one). Second, it tells us which line and character it’s having trouble with (namely line 4, characters 25-26): this is the
List.fold_left min a b;;.
What problem is it encountering? Well, according to the error message, it’s expecting the
b to have type
(int -> 'a) list, that is, to be a list of functions from
int to values of type
'a (whatever type
'a turns out to be). But instead it’s finding
b to have just type
Study the type of
- : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a = <fun>
And then, thinking algebraically, try to determine the types of the values you are applying this function to. To do that, try evaluating the sub-expressions in your code, using different dummy data in place of
lst, and see if they have the types and values that you expect. E.g.,
- : 'a list -> int -> 'a = <fun>
# let a = List.nth [1;2;3];;
val a : int -> int = <fun>
- : 'a list -> 'a = <fun>
# let b = List.hd [1;2;3];;
val b : int = 1
Are you expecting
a to be a function? It’s ending up as a function because you’re partially applying
List.nth. Is that what you intended?
Here’s a correct implementation of a function to find the minimum value in a list:
# let min_list lst = List.fold_left min (List.hd lst) (List.tl lst);;
val min_list : 'a list -> 'a = <fun>
Compare it with yours, and see if you can figure out where things are going wonky.
Please feel free to add followup questions, and, once you’ve figured out why your code isn’t working, it would be great if you’d post here explaining what you’ve figured out!