You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用CPS风格编写OCaml中的fold_left函数?

OCaml中fold_left的CPS版本实现研究

尽管fold_left本身已是尾递归函数,但实现其CPS版本仍具有研究价值。本次实现基于OCaml语言。

标准fold_left实现

let rec myFoldLeft f acc list =
  match list with
  | [] -> acc
  | h :: tl -> myFoldLeft f (f h acc) tl
;;

fold_right的CPS版本参考示例

let rec myFoldRightCPS f acc list cont =
  match list with
  | [] ->
      cont acc
  | h :: tl ->
      myFoldRightCPS f acc tl (fun t -> cont @@ f t h)

内容的提问来源于stack exchange,提问作者utSabbath

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 10:34:58