Reputation: 1
let my_matrix1 = [[1;2];[3;4]];;
let my_matrix2 = [[4;7;8];[3;2;1]];;
I have defined two matrices so I can see if the code is working properly.
I have done it for the element:
let rec does_it_contain lis x =
if (lis = []) then false
else if ((List.hd lis) = x) then true
else does_it_contain (List.tl lis) x;;
let rec does_it matrix x =
if (matrix = []) then false
else if (does_it_contain (List.hd matrix) x = true) then true
else does_it (List.tl matrix) x;;
does_it my_matrix1 2;;
- bool = true
And the problem is working, but I'm not sure what went wrong when I tried to do it with the matrices (if one contains the other):
let rec does_it_contain lis1 lis2 =
if (List.hd lis1 = []) then false
else if (List.hd lis1 = lis2) then true
else does_it_contain (List.tl lis1) lis2;;
let rec does_it matrix lis1 =
if (matrix = []) then false
else if (does_it_contain List. hd matrix List.hd lis1 = true) then true
else does_it (List.tl matrix) lis1;;
does_it my_matrix1 my_matrix2;;
Please help
Upvotes: 0
Views: 499
Reputation: 1596
I think you must clarify what you're meaning by "one matrix is contained in the other matrix".
Regarding at you're code i assume that the order of the elements in the matrix does not matter, and you just want to check if all the elements of one matrix appear in the other one. Can you confirm that?
Moreover, you are not really saying what your problem is. compile-time error? not the good result? From what i see, you have at least an error due to :
does_it_contain List. hd matrix List.hd lis1
where you should put parenthesis around List. hd matrix
and List.hd lis1
to avoid the applied to too many argument error.
Let's end with few coding-style remarks :
1) You can use List.mem
instead of redefining it here :
let rec does_it_contain lis x =
if (lis = []) then false
else if ((List.hd lis) = x) then true
else does_it_contain (List.tl lis) x;;
2) This ...
if (does_it_contain List. hd matrix List.hd lis1 = true) then true
else blabla
... is very poor style. you can directly write
does_it_contain (List.hd matrix) (List.hd lis1) || blabla
3) It's prefered to use pattern-matching instead of if (lis = []) then false else if ((List.hd lis) = x) ...
. It will check statically for you if you're missing some cases or if you are writing redundant ones.
Prefer :
match lis with
| [] -> false
| h::t -> ...
Edit : Working with matrix is an adapted task for array, so my approach would be to convert your list to an array an then use the classical imperative approach. If you want to stick to the lists, than you need to make appear the index of the found sublines in your matrix : if all the line of the matrix A, appear in the matrix B, AND at the same begining index, then A is a submatrix of B.
one of the problems of your code is (beside the fact that the following won't compile) is : if (does_it_contain List. hd matrix List.hd lis1 = true) then true
Here, you return true whenever the begining element of your matrix is the same than the one of your list. You should here check for all the elements and not only the head.
if (does_it_contain (List.hd matrix) (List.hd lis1) = true) then
does_it (List.tl matrix) lis1
else false
This makes you closer to the solution but wont fix your code. You should make the index appear in your computation.
Upvotes: 2