【发布时间】:2019-04-30 11:43:39
【问题描述】:
我想编写一个函数来添加两个 eval 类型的元素
type eval = Num of float | Neg | Add | Sub | Mul | Div;;
OCaml 编译器给了我这个警告,但我不知道它到底想要什么。它有效,但我想在没有这个警告的情况下成功。
# let (+++) (Num a) (Num b) =
match (Num a), (Num b) with
| (Add|Neg|Sub|Mul|Div), _ -> failwith "01"
| _, (Add|Neg|Sub|Mul|Div) -> failwith "02"
| _, _ -> Num (a +. b)
;;
Warning 8: this pattern-matching is not exhaustive.
Here is an example of a case that is not matched:
(Neg|Add|Sub|Mul|Div)
Warning 8: this pattern-matching is not exhaustive.
Here is an example of a case that is not matched:
(Neg|Add|Sub|Mul|Div)
val ( +++ ) : eval -> eval -> eval = <fun>
【问题讨论】:
-
尝试使用
Num以外的任何其他方式调用您的+++函数。您的failwith "01"和failwith "02"案例将永远无法处理。
标签: function functional-programming ocaml