【发布时间】:2015-04-20 05:42:53
【问题描述】:
我正在将我的一些 Clojure 代码移植到 OCaml,但遇到了以下问题:
let rotate ll =
let cons x y = x :: y in
let init = List.map (fun _ -> []) (List.hd ll) in
let rres = List.fold_right (List.map2 cons) ll init in
List.rev rres;;
let rec spiral_print matrix acc =
match matrix with
| [] -> acc
| head :: tail -> spiral_print (rotate tail) (acc @ head);;
不幸的是,这会导致以下结果:
utop # spiral_print [[1; 2; 3]; [8; 9; 4]; [7; 6; 5]] [];;
Exception: (Failure hd).
我想调试为什么会发生这种情况,但是当我打开跟踪时,这就是我得到的:
#trace spiral_print;;
spiral_print [[1; 2; 3]; [8; 9; 4]; [7; 6; 5]] [];;
spiral_print <-- [[<poly>; <poly>; <poly>]; [<poly>; <poly>; <poly>]; [<poly>; <poly>; <poly>]]
spiral_print --> <fun>
spiral_print* <-- []
spiral_print <-- [[<poly>; <poly>]; [<poly>; <poly>]; [<poly>; <poly>]]
spiral_print --> <fun>
spiral_print* <-- [<poly>; <poly>; <poly>]
spiral_print <-- [[<poly>; <poly>]; [<poly>; <poly>]]
spiral_print --> <fun>
spiral_print* <-- [<poly>; <poly>; <poly>; <poly>; <poly>]
spiral_print <-- [[<poly>]; [<poly>]]
spiral_print --> <fun>
spiral_print* <-- [<poly>; <poly>; <poly>; <poly>; <poly>; <poly>; <poly>]
spiral_print <-- [[<poly>]]
spiral_print --> <fun>
spiral_print* <-- [<poly>; <poly>; <poly>; <poly>; <poly>; <poly>; <poly>; <poly>]
spiral_print* raises (Failure hd)
spiral_print* raises (Failure hd)
spiral_print* raises (Failure hd)
spiral_print* raises (Failure hd)
spiral_print* raises (Failure hd)
Exception: (Failure hd).
有没有办法以某种方式检查各个函数执行的内部状态以捕捉错误?
我会在其他语言中使用 print 或 pretty print。
【问题讨论】: