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.
如何在 ML 中返回递归函数?
据我所知,不能返回递归匿名函数,只有匿名函数可以用作返回值(如果返回值是函数......)。
这会是你想知道的那种例子吗?
fun f n = let fun g k = if k = n then [] else k :: g (k-1) in g end
您只能通过命名来进行递归定义,但这不是问题,因为您可以在let任何地方编写表达式。
let
更新以更具体地回答评论:
fun f g = let fun h 0 = g 0 | h i = h (i-1) + g i in h end
(更有效的实现将使h尾递归。)
h