Lamebro1
Lamebro1

Reputation: 57

Ocaml - Checking that all rows of a matrix are the same length

i'm trying to find a way in ocaml to apply functions to matrices.

In my implementation, a matrix is an int list of an int list (as in the matrix is a int list, which holds other int lists)

My idea right now is:

let valid_matrix x =
  match x with
  [] -> true
  | (map length x) ->
 ;;

(Map and length are separate functions which find the number of elements in a list and perform a map function in which a function is applied to all elements of a list, i cannot use a library).

I know this code is incomplete but i am unsure how to compare the number of elements in each matrix to each other and I don't know either how I would access each individual int list (row) within the matrix if I wanted to do further manipulation to it.

Kind regards.

Upvotes: 1

Views: 814

Answers (1)

Goswin von Brederlow
Goswin von Brederlow

Reputation: 12342

"i cannot use a library" => so this is homework. No code for you, just hints.

Split your problem in two:

1) Change the rows of the matrix into their length. For this apply the length function to each row of the matrix (your map function). This then gives you a int list containing the length of each row.

2) Check that all values in the list are the same.

Upvotes: 1

Related Questions