I have been learning OCaml and have been trying to write a function that takes in a list of lists of type int, which are representing a matrix. For example: [[1;2;3]; [4;5;6]; [7;8;9]] and in return I want to return a bool value indicating if it is a proper matrix, or not. The way this is decided is if all rows in the matrix have the same amount of elements (like the example shown above)
Thus, I have created the following function:
let rec matrix lst =
match lst with
| h::t ->
(match h with
| a ->
if (List.length a = List.length (matrix t)) then true else false)
My function type is not what I'm expecting. It should just be
is_matrix : (int list) list -> bool
or the general equivalent with a'
I'm getting a compile error saying: the variant type list has no constructor true Any idea what's that about?
let rec getlength x =
match x with
| [] -> 0
| a::b -> List.length a + getlength b
let matrix lst =
match lst with
| [] -> true
| h::t -> if (getlength h = getlength t) then true else false