匹配失败问题OCaml
我的OCaml代码出现匹配失败,我不知道可能是什么问题。我试图只处理一个案例,并从中找出问题出在哪里,但我收到的错误是:匹配失败问题OCaml,ocaml,Ocaml,我的OCaml代码出现匹配失败,我不知道可能是什么问题。我试图只处理一个案例,并从中找出问题出在哪里,但我收到的错误是: Exception: Match_failure ("hw2.ml", 49, 0). 代码: let rec compileStack(il : instr list) (st : float list) = match (il,st) with [],[_] -> List.hd st |((Push f)::t),[_] -> compileStac
Exception: Match_failure ("hw2.ml", 49, 0).
代码:
let rec compileStack(il : instr list) (st : float list) =
match (il,st) with
[],[_] -> List.hd st
|((Push f)::t),[_] -> compileStack t (f::st)
|(Swap)::t, h2::h1::st -> compileStack t (h2::h1::st)
|(Calculate op)::t, h1::h2::st ->
match op with
Plus -> compileStack t (h2+.h1::st)
| Minus -> compileStack t (h2-.h1::st)
| Times -> compileStack t (h2*.h1::st)
| Divide -> compileStack t (h2/.h1::st) ;;
let execute (li : instr list) : float =
let stack = [] in
compileStack li stack;;
如果您有任何建议,我们将不胜感激,我们已经为此坚持了2个小时了 编译时,请注意编译器的输出。如果它说
Warning ...: this pattern-matching is not exhaustive.
那通常意味着你跳过了一些案子。顺便说一下,编译器提供了一个遗漏案例的示例
考虑到你的问题,我会把不同的工作分成不同的功能——这会让你更容易处理这些情况;另外,不要忘记堆栈下溢,当堆栈中没有足够的数据执行交换或二进制算术运算时,就会发生这种情况。请参见下面的示例
(* just type aliases for convenience *)
type stack = float list
type binop = float -> float -> float
(* helper function to prevent nested pattern matching below *)
let denote_operation (op : oper) : binop =
match op with
| Plus -> ( +. )
| Minus -> ( -. )
| Times -> ( *. )
| Divide -> ( /. )
(* This function executes only 1 instruction and
returns 'stack option', which may signal stack underflow *)
let execute_instruction (i : instr) (st : stack) : stack option =
match i with
| Push f -> Some (f :: st)
| Swap ->
(match st with
| y :: x :: st' -> Some (x :: y :: st')
| _ -> None)
| Calculate op ->
(match st with
| y :: x :: st' -> Some ((denote_operation op x y) :: st')
| _ -> None)
(* this function deals with a bunch of instructions *)
let rec execute_program (il : instr list) (st : stack) : stack option =
match il with
| [] -> Some st
| i :: il' ->
match (execute_instruction i st) with
| None -> None
| Some st' -> execute_program il' st'
编译时,请注意编译器的输出。如果它说
Warning ...: this pattern-matching is not exhaustive.
那通常意味着你跳过了一些案子。顺便说一下,编译器提供了一个遗漏案例的示例
考虑到你的问题,我会把不同的工作分成不同的功能——这会让你更容易处理这些情况;另外,不要忘记堆栈下溢,当堆栈中没有足够的数据执行交换或二进制算术运算时,就会发生这种情况。请参见下面的示例
(* just type aliases for convenience *)
type stack = float list
type binop = float -> float -> float
(* helper function to prevent nested pattern matching below *)
let denote_operation (op : oper) : binop =
match op with
| Plus -> ( +. )
| Minus -> ( -. )
| Times -> ( *. )
| Divide -> ( /. )
(* This function executes only 1 instruction and
returns 'stack option', which may signal stack underflow *)
let execute_instruction (i : instr) (st : stack) : stack option =
match i with
| Push f -> Some (f :: st)
| Swap ->
(match st with
| y :: x :: st' -> Some (x :: y :: st')
| _ -> None)
| Calculate op ->
(match st with
| y :: x :: st' -> Some ((denote_operation op x y) :: st')
| _ -> None)
(* this function deals with a bunch of instructions *)
let rec execute_program (il : instr list) (st : stack) : stack option =
match il with
| [] -> Some st
| i :: il' ->
match (execute_instruction i st) with
| None -> None
| Some st' -> execute_program il' st'
失败测试的输入数据是什么?顺便说一句,(il,st)上的模式匹配并不是详尽无遗的:您错过了很多案例。这似乎是你的问题。失败测试的输入数据是什么?顺便说一句,(il,st)上的模式匹配并不是详尽无遗的:您错过了很多案例。这似乎是你的问题。