我试图在除以0时捕获异常,但不管怎样实现,代码都没有显示任何实质性的内容,始终声称结果是“无穷大”(从我的理解来看,这意味着它只执行了除法并忽略了其他所有内容)
这是什么原因,如何解决?
open System
type instruction =
| ADD
| SUB
| MUL
| SQR
| DIV
| PUSH of float
type stack = float list
exception BLEDNY_PROGRAM of (instruction * stack)
exception DivideByZeroException
let intInstr (x, y) =
match x, y with
| ADD, a::b::ys -> (b + a) :: ys : stack
| SUB, a::b::ys -> (b-a)::ys
| MUL, a::b::ys -> (b*a)::ys
| SQR, a::ys -> (a * a)::ys
| DIV, a::b::ys -> try (b/a)::ys with | :? System.DivideByZeroException -> (printf "Błąd: dzielenie przez zero"; ys)
| PUSH x, ys -> x::ys
| _ , _ -> raise (BLEDNY_PROGRAM(x, y));
let intpProg(is) =
let rec iPS = function
| ([],x::xs) -> x
| (i::is, xs) -> iPS(is, intInstr(i, xs))
iPS(is,[])
let il3 = [PUSH 3.0; PUSH 0.0; DIV];
let e = intpProg(il3)
printfn "%A" e