0
    let moja_matrika1 = [[1; 2]; [3; 4]];;
let moja_matrika2 = [[4; 7; 8]; [3; 2; 1]];;

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 (moja_matrika1, moja_matrika2);;

I am trying to check if a matrix is sbumatrix of another matrix. But I have to use type list list. And a I can't use any of the defined List functions. Obviosly I am using List hd, tl but i will replace that.

A get an error when I try to call the function wich i dont understand.

   does_it (moja_matrika1, moja_matrika2);;
Error: This expression has type int list list
       but an expression was expected of type 'a list list list
       Type int is not compatible with type 'a list # 

Please help!

Broda
  • 15
  • 7
  • "_But I have to use type list list_": If you do that for anything else than learning, you should not use lists to represent matrices, this is very inefficient. – ChriS Apr 14 '17 at 06:33
  • I know, this was for school. – Broda Apr 14 '17 at 13:47

2 Answers2

0

The signature of does_it_contain and does_it are respectnot ively :

  'a list list * 'a list -> bool 
  'a list list list * 'a list list -> bool  

None of them are capable to take 2 matrix as arguments ('a list and 'a list list list are not matrix as you expect )

Also, you write the code of your function as if it were C or java : the argument are not to be passed through parenthesis in Ocaml. Parenthesis are used for tuples - which is not useful in that case.

Pierre G.
  • 4,346
  • 1
  • 12
  • 25
  • So, It's useless, or? I'm sorry but I am a beginner at Ocaml. I wrote Delphi, Java, C. But Ocaml is just something else. I can't seem to wrap my head around it. Any suggestions on how to improve this code, make it work, if it's possbile? Thanks I – Broda Apr 13 '17 at 20:07
  • in first place, it is useless. But it is also adding some penalty to your code as it creates another data structure (the tuple) that you destructure to work on each of the element. On bigger code it will increase the time complexity - could be even the case on your code with big matrix. – Pierre G. Apr 14 '17 at 01:38
0

I figured it out.

let rec does_it_contain (lis1, lis2) =
    if rep lis1 = [] then false
    else if rep lis2 = [] then false
  else if (  glava lis1 = glava lis2) then true
 else does_it_contain ( lis1, rep lis2);;

let rec does_it (matrica, matrica1) =
    if rep matrica = [] then false
    else if rep matrica1 = [] then false
    else if does_it_contain ( glava matrica, glava matrica1) = true then true
    else  does_it ( rep matrica,  rep matrica1);;


does_it(moja_matrika1, moja_matrika2);;

rep being an alternative for List.tl and glava an alternative for List.hd.

Broda
  • 15
  • 7