我想问如果异常:“异常STACK_OVERFLOW”可能导致无限循环,特别是异常下面的代码出现:异常“STACK_OVERFLOW” OCaml中
(*the loop "while" should stop when both stacks are empty*)
while (not (Stack.is_empty stackFalse))|| (not (Stack.is_empty stackTrue)) do
(
if (not (Stack.is_empty stackTrue)) then
(
let q1 = Stack.pop stackTrue in
let (_,_,ptrs) = fst (Hashtbl.find graph (fst q1)) in
List.iter (fun elem ->
let app = Hashtbl.find graph elem in
let (typeNode,last,ptrs') = fst app in
if typeNode = "Or-node" then
(
Stack.push (elem,true) stackTrue;
Hashtbl.add labeled elem true
)
else if last = false then
Hashtbl.replace graph elem ((typeNode,true,ptrs'),snd app)
else
(
Stack.push (elem,true) stackTrue;
Hashtbl.add labeled elem true
) ) ptrs ;
);
if (not (Stack.is_empty stackFalse)) then
(
let q2 = Stack.pop stackFalse in
let (_,_,ptrs1) = fst (Hashtbl.find graph (fst q2))in
List.iter (fun elem ->
let app = Hashtbl.find graph elem in
let (typeNode,last,ptrs') = fst app in
if typeNode = "And-node" then
(
Stack.push (elem,false) stackFalse;
Hashtbl.add labeled elem false
)
else if last = false then
Hashtbl.replace graph elem ((typeNode,true,ptrs'),snd app)
else
(
Stack.push (elem,false) stackFalse;
Hashtbl.add labeled elem false
) ) ptrs1 ;
);
)
done;
为了更加习惯于函数式编程,你应该用一个列表替换你的'Stack',并用递归调用替换'while'循环。是否有理由将本节编程为强制性风格? – nlucaroni 2011-01-05 15:04:20
通常不会,while循环不会导致堆栈溢出 - 没有堆栈。 – 2011-01-05 16:40:31