OCaml - 警告 8:这种模式匹配并不详尽

问题描述

你好,我的程序运行良好,但给我一个警告,我想摆脱它

  let rec replace_helper (x::xs) n acc =
    if n = 0 then
      List.rev acc @ symbol :: xs
    else
      replace_helper xs (pred n) (x :: acc)
    in
    replace_helper tape position []
  ;;

这是警告

Warning 8: this pattern-matching is not exhaustive.
Here is an example of a case that is not matched:[]

我该怎么做才能摆脱这种情况

解决方法

你的 x::xs 模式只能匹配一个非空列表(你忘记处理空列表 [] 的情况)。请填写以下代码

let rec replace_helper li n acc = match li with
   [] -> (*code something here*) failwith "incomplete"
 | x::xs ->
   if n = 0 then
     List.rev acc @ symbol :: xs
   else
      replace_helper xs (pred n) (x :: acc)
   in
   replace_helper tape position []
 ;;

当然还有阅读更多http://ocaml.org/