语法错误
type directive = TurnLeft | TurnRight | StepForward of int | StepBackward of int
type path = directive list
let sample_path = (StepForward 1 :: StepForward 2 :: TurnLeft:: StepBackward 3 :: TurnLeft :: StepForward 1 ::[])
let inverse directive =
if directive =TurnLeft then TurnRight
else if directive = TurnRight then TurnLeft
else if directive = (StepForward of int) then (StepBackward of int)
else (StepForward of int)
;;有人能看到这里的问题吗?该函数应用于反转directive的每个元素。例如:TurnLeft -> TurnRight和StepForward 3 -> StepForward 3。
发布于 2022-11-09 13:40:00
分析数据形状的OCaml方法是使用模式匹配:
let inverse directive = match directive with
| TurnRight -> ...
| TurnLeft -> ...
| Stepforward n -> ...
| Stepbackward n -> ...特别是:
x = y是一个没有前面的等式测试,让Constructor of type_expression只能在定义变体类型时使用:type t = A of int并且不是语法上有效的表达式(或模式)。
https://stackoverflow.com/questions/74374449
复制相似问题