Error when convert to Boolean matrix

2019-08-01 12:02发布

问题:

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"])]

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"]

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"]

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

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

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...

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)

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

It return an error "Fatal error: exception Not_found"

I need your help. Thank you very much!!

回答1:

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

and matrix is calculated as:

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

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



标签: ocaml