我应该只实现尾递归函数。考虑到每次调用时我都有三个函数在其中工作以获得答案,这个代码尾递归吗?
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
请考虑一下:"某物"和"某物"都是尾递归函数。我确信这一点。
anyfunction
,按照定义,是尾递归的,无论someotherthing
和something
是什么。
唯一取决于someotherthing
和something
是否是尾递归的,是anyfunction
将从尾调用优化中受益多少。