【问题标题】:Can I call the following code "Tail Recursive"? -Haskell function我可以将以下代码称为“尾递归”吗? -Haskell 函数
【发布时间】:2020-10-02 12:08:28
【问题描述】:

我应该只实现尾递归函数。考虑到在每次调用中我都有三个函数在其中工作以获得答案,这段代码是否是尾递归的?

anyfunction :: (Ord a) => Int -> [a] -> [a] -> [a] -> a
anyfunction n [] ys ws = anyfunction n ws ys ws
anyfunction (-1) (x:xs) ys ws = something x xs
anyfunction n (x:xs) ys ws = anyfunction (n+1) (someotherthing(something x xs) (x:xs) []) (ys ++ [(something x xs)]) ws

请注意:'something' 和 'someotherthing' 都是尾递归函数。我确信这一点。

【问题讨论】:

  • 如果您确定anyfunction 是尾递归的,那么您的问题是什么?
  • 所以你确定anyfunction是尾递归的,而你的问题是anyfunction是否是尾递归?

标签: haskell recursion optimization tail-recursion tail


【解决方案1】:

anyfunction,按照定义,是尾递归的,不管someotherthingsomething 是什么。

唯一取决于someotherthingsomething 是否为尾递归的是anyfunction 将从尾调用优化中受益多少。

【讨论】:

    猜你喜欢
    • 2017-05-23
    • 2023-03-25
    • 1970-01-01
    • 2012-12-30
    • 2021-07-27
    • 1970-01-01
    • 1970-01-01
    • 2013-05-05
    相关资源
    最近更新 更多