转换为布尔矩阵时出错 [英] Error when convert to Boolean matrix

查看:78
本文介绍了转换为布尔矩阵时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个问题仍然困扰着我很多.我有一个(string * string list) list,我想将其转换为布尔矩阵.

I have this problem still bother me a lots. I have a (string * string list) list and I want to convert it to Boolean matrix.

转换时我有一个特殊条件.例如,我有此列表:

I have a special condition when transform it. For example I have this list:

let entries = [("name", ["string"; "label"]); ("label", ["int"; "name"]); 
               ("symbol", ["string"])]

其中"string"和"int"是未定义类型,未定义类型,因为在我的实际数据中,我没有定义来描述此类型.因此,我建立了一个未定义类型的列表.

where "string" and "int" are undefined type, undefined type because in my real data, I don't have a definition describe this type. So I built a list of undefined type.

let undefined = ["string"; "int"]

列表中的第一个位置("name","label","symbol")是定义的类型,定义的类型是我在数据中定义的类型.

And the first position in the list ("name", "label", "symbol") are defined type, defined type is the type I have definition in my data.

let defined = ["name"; "label"; "symbol"]

我正在尝试这样做:从entries开始,应该有位置:

I am trying to do this: from entries, there position should be:

name: 2; string: 0; label: 3; int: 1; symbol: 4

并且当显示列表entries中的依赖关系时,它不会更改其位置.例如:name(2)链接到string(0)label(3),而label (3)则具有到int(1)name (2)的边缘,依此类推...

And when showing the depend relation from the list entries, it doesn't change their position. For example: name(2) link to string(0) and label(3), and label (3) has an edge to int(1) and name (2),` and so on...

我有这些函数返回列表中的位置(num_of_name)和元素(name_of_num).

I have these functions return a position(num_of_name) and element (name_of_num) in a list.

let rec position x = function
| [] -> raise Not_found
| y :: ys -> if x = y then 0 else 1 + position x ys

let len_undefined = List.length undefined

let num_of_name defined undefined len_undefined s =
  try (position s defined) + len_undefined;
  with Not_found -> position s undefined

let name_of_num defined undefined len_undefined k =
  if k < len_undefined then
    List.nth undefined k else
    List.nth defined (k - len_undefined)

所以从entries列表中,我想使用函数num_of_name建立一个布尔矩阵,显示那里的关系.所以我写了我的函数:

So from the entries list I want to build a boolean matrix show there relation using the function num_of_name. So I write my function:

let matrix =
  let len = List.length defined + len_undefined in
  let boolmat = Array.make_matrix len len false in
  List.iter (fun (s, strs) ->
    let pos1 = num_of_name defined undefined len_undefined s in
      List.iter (fun t ->
    let pos2 = num_of_name defined undefined len_undefined t in
    boolmat.(pos1).(pos2) <- true) strs) entries;
    boolmat

let print_mat m =
  for i = 0 to Array.length m - 1 do
    for j = 0 to Array.length m.(0) - 1 do
      print_string (string_of_bool m.(i).(j));
      Printf.printf " ";
    done;
    Printf.printf " \n";
  done;
;;

let test_print = print_mat matrix

返回错误"Fatal error: exception Not_found"

我需要你的帮助. 非常感谢!!

I need your help. Thank you very much!!

推荐答案

正如我在评论中所说,您的num_of_name函数是脆弱的,因为当其输入不是defined的任何元素时,它会抛出Not_found异常.或undefined.解决方法之一是使用Option类型:

As I said in the comment, your num_of_name function is fragile since it throws Not_found exception when its input is not an element of either defined or undefined. One way to fix is using Option type:

let num_of_name defined undefined len_undefined s =
  try 
      let p = position s defined in
      Some (p + len_undefined)
  with Not_found -> 
     try
       let p = position s undefined in
       Some p
     with Not_found -> None

matrix的计算公式为:

let matrix =
  let len = List.length defined + len_undefined in
  let boolmat = Array.make_matrix len len false in
  List.iter (fun (s, strs) ->
    match num_of_name defined undefined len_undefined s with
    | Some pos1 -> List.iter (fun t ->
                      match num_of_name defined undefined len_undefined t with
                      | Some pos2 -> boolmat.(pos1).(pos2) <- true
                      | None -> ()) strs
    | None -> ()
      ) entries; 
  boolmat

当然,如果通过从entries中提取definedundefined来执行程序,则代码是正确的.

Of course, if you enforce your program by extracting defined and undefined from entries, your code is correct.

这篇关于转换为布尔矩阵时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆