子矩阵Ocaml

时间:2017-04-13 19:52:54

标签: matrix ocaml submatrix

    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);;

我正在尝试检查矩阵是否是另一个矩阵的sbumatrix。但我必须使用类型列表。我不能使用任何已定义的List函数。显然我正在使用List hd,tl但我将替换它。

当我尝试调用我不理解的函数时出错。

   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 # 

请帮忙!

2 个答案:

答案 0 :(得分:0)

do_it_contain和does_it的签名非常不尊重:

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

他们都没有能够将2个矩阵作为参数('a list'a list list list不是你期望的矩阵)

此外,您将函数的代码编写为C或java:该参数不会通过Ocaml中的括号传递。括号用于元组 - 在这种情况下无用。

答案 1 :(得分:0)

我明白了。

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是List.tl的替代品,glava是List.hd的替代品。