【发布时间】:2021-05-05 15:50:38
【问题描述】:
我有一个名为 zip_with_2_fs 的函数。我试图让它尾递归(zip_with_2_fs_tr),但我真的不明白我在做什么。我是 Ocaml 的新手,想深入了解尾递归和辅助函数。
let rec zip_with_2_fs fx fy xs ys =
match (xs, ys) with
| ([], []) -> []
| ([], _) -> []
| (_, []) -> []
| (xh::xt, yh::yt) -> (fx xh, fy yh)::zip_with_2_fs fx fy xt yt;;
我将如何解决这个问题?
let zip_with_2_fs_tr fx fy xs ys =
let rec helper fxx fy xss yss =
match (xs, ys) with
| ([], []) -> []
| ([], _) -> []
| (_, []) -> []
| (xh::xt, yh::yt) -> (fx xh, fy yh)::helper fx fy xt yt
in helper fx fy xs ys;;
【问题讨论】:
-
您的问题是什么?你读过关于tail calls 的维基百科吗?您的
zip_with_2_fs_tr不是 尾递归的,因为::是在递归调用之后 完成的!注意continuation-passing style
标签: ocaml