是什么导致以下代码中的stackoverflow

What is causing stackoverflow in the following code

我不明白为什么我在以下代码中不断收到 Whosebug 错误。是什么原因造成的?

let rec fl x y ct = match x with
    [] -> []
  | (h::t) -> if haskey ct y = true then (fl x y (ct + 1))
    else h :: (fl x y (ct + 1))
;;

您的 fl 函数无限递归到自身:

let rec fl x y ct = match x with
    [] -> []
  | (h::t) -> if haskey ct y = true then (fl x y (ct + 1))
    else h :: (fl x y (ct + 1))
;;

递归调用的唯一变化是对ct的增量。据推测,您需要 ct 的某种终止条件,或者您需要使用列表的尾部调用 fl。