Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
以下代码旨在附加两个列表。
fun {AppendLists L1 L2} if L1 == nil then L2 else L1.1 | {AppendLists L1.2 L2} end end
如果你不明白为什么你的代码已经是尾递归的,这里是相同的代码,但语法糖少了一点。您的函数已转换为带有额外参数的过程以存储结果(这由兼性“?”表示)。
proc {AppendLists L1 L2 ?R} if L1 == nil then L2 else K in R = L1.1 | K {AppendLists L1 L2 K} end end
此代码已经是尾递归的。您将递归作为最后一条语句,因此您首先计算结果,然后调用递归。