在 OCaml 中将一对 int 列表转换为布尔矩阵

发布于 2024-12-22 08:58:49 字数 1253 浏览 4 评论 0原文

我有一个函数将 (int * int list) list 转换为布尔矩阵。我用我的反例进行了测试,它给了我一个正确的答案。就像下面的代码一样。

let to_matrix l =
let n = List.length l in
  let m = Array.make_matrix (n + 1) (n + 1) false in
  List.iter (fun (i, j) ->
  List.iter (fun t ->
    m.(i).(t) <- true) j) l;
  m;;

let ex = to_matrix [(0, [1; 0]); (1, [0]); (2, [1])];;

它给了我一个矩阵:

true true false false  
true false false false  
false true false false  
false false false false

我必须在真实数据中测试它,它是一个 xsds 列表,如下面的数据脚本。例如:

[Elt ("name", Some (SimpleType "string"), 1, Bound 1);
   Group ("label",
    Some
     (Choice
       [Elt ("numberLabel",
         Some
          (Sequence
            [Elt ("number", Some (SimpleType "nonNegativeInteger"), 0,
              Unbounded)]),
         1, Bound 1);
        Elt ("symbolLabel",
         Some (Sequence [GroupRef ("name", 0, Unbounded)]), 1, Bound 1)]),
    1, Bound 1)]

编辑:

感谢托马斯的回答。

我错过了关于我遍历这些树返回的类型的解释。

遍历后的类型看起来像这样的列表:

[("name"; ["string"]); ("label"; ["nonNegativeInteger"; "name"])...]

从这个 xsds 列表中,我想表示一个布尔矩阵以显示它们之间的依赖关系,例如:类型名称取决于类型字符串;类型 标签取决于类型 nonNegativeInteger 和名称。

I have a function that taking (int * int list) list to a boolean matrix. I tested with my counter example and it gave me a correct answer. Like code below.

let to_matrix l =
let n = List.length l in
  let m = Array.make_matrix (n + 1) (n + 1) false in
  List.iter (fun (i, j) ->
  List.iter (fun t ->
    m.(i).(t) <- true) j) l;
  m;;

let ex = to_matrix [(0, [1; 0]); (1, [0]); (2, [1])];;

It gives me a matrix:

true true false false  
true false false false  
false true false false  
false false false false

I have to test it in my real data, it is an xsds list, like data script below. For example:

[Elt ("name", Some (SimpleType "string"), 1, Bound 1);
   Group ("label",
    Some
     (Choice
       [Elt ("numberLabel",
         Some
          (Sequence
            [Elt ("number", Some (SimpleType "nonNegativeInteger"), 0,
              Unbounded)]),
         1, Bound 1);
        Elt ("symbolLabel",
         Some (Sequence [GroupRef ("name", 0, Unbounded)]), 1, Bound 1)]),
    1, Bound 1)]

EDIT:

Thanks for the answer from Thomas.

I missed the explanation about the type I returned from traverse these trees.

The type after traverse it look like this list:

[("name"; ["string"]); ("label"; ["nonNegativeInteger"; "name"])...]

From this xsds list I would like to represent an boolean matrix to show the dependence between them, for example: type name depend on type string; type
label depend on type nonNegativeInteger and name.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人心善变 2024-12-29 08:58:49

函数to_matrix是错误的。

如果您使用 to_matrix [(0, [2])];; 测试它,您就可以看到它。

为了使其工作,您需要找到一种方法来映射 'a< /code> 到 [0..(n-1)],其中 n 是列表中出现的元素数量。

一种可能的方法是使用关联列表:

let mapping = ref []
let int_of_a a =
  if List.mem_assq a !mapping then
    List.assq a !mapping
  else
    let n = List.length !mapping in
    mapping := (a, n) :: !mapping;
    n

并将您的 to_matrix 函数更改为:

let to_matrix l =
  (* step 1: registering your inputs *)
  mapping := [];
  let register i = ignore (int_of_a i) in
  List.iter (fun (i, j) ->
    register i;
    List.iter register j
  ) l;
  let n = List.length !mapping in
  (* step 2: creating and populating your matrix *)
  let m = Array.make_matrix n n false in
    List.iter (fun (i, j) ->
      List.iter (fun t ->
        m.(int_of_a i).(int_of_a t) <- true
      ) j
    ) l;
    m;;

这显然不是编写此类内容的最佳方式,但您明白了。

The function to_matrix is wrong.

You can see it if you test it with to_matrix [(0, [2])];;

In order to make it work, you need to find a way to map the 'a of your list to [0..(n-1)] where n is the number of elements appearing in your list.

A possible way it to use an association list:

let mapping = ref []
let int_of_a a =
  if List.mem_assq a !mapping then
    List.assq a !mapping
  else
    let n = List.length !mapping in
    mapping := (a, n) :: !mapping;
    n

and to change your to_matrix function to be:

let to_matrix l =
  (* step 1: registering your inputs *)
  mapping := [];
  let register i = ignore (int_of_a i) in
  List.iter (fun (i, j) ->
    register i;
    List.iter register j
  ) l;
  let n = List.length !mapping in
  (* step 2: creating and populating your matrix *)
  let m = Array.make_matrix n n false in
    List.iter (fun (i, j) ->
      List.iter (fun t ->
        m.(int_of_a i).(int_of_a t) <- true
      ) j
    ) l;
    m;;

That's clearly not the best way to write this kind of things, but you get the idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文