Search code examples
haskellrecursionoptimizationtail-recursiontail

Can I call the following code "Tail Recursive"? -Haskell function


I am supposed to implement tail recursive functions only. Is this code tail recursive, considering that at each call I have three functions working inside it for the answer?

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

Please, consider that: 'something' and 'someotherthing' are all tail recursive functions. I am sure of that.


Solution

  • anyfunction, as defined, is tail-recursive, no matter what someotherthing and something are.

    The only thing that depends on whether someotherthing and something are tail-recursive is how much anyfunction will benefit from tail-call optimization.