在 OCaml 中将一对 int 列表转换为布尔矩阵
我有一个函数将 (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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
函数
to_matrix
是错误的。如果您使用
to_matrix [(0, [2])];;
测试它,您就可以看到它。为了使其工作,您需要找到一种方法来映射
'a< /code> 到
[0..(n-1)]
,其中n
是列表中出现的元素数量。一种可能的方法是使用关联列表:
并将您的
to_matrix
函数更改为:这显然不是编写此类内容的最佳方式,但您明白了。
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)]
wheren
is the number of elements appearing in your list.A possible way it to use an association list:
and to change your
to_matrix
function to be:That's clearly not the best way to write this kind of things, but you get the idea.