I want to know if this represents a tail-recursion. And if it isn’t how can I do it.
countP :: [a] -> (a->Bool) -> Int
countP [] _ = 0
countP (x:xs) p = countP_aux (x:xs) p 0
countP_aux [] _ _ = 0
countP_aux (x:xs) p acumul
|p x==True = (countP_aux xs p (acumul))+1
|otherwise = (countP_aux xs p (acumul))
countP [1,2,3] (>0)
3
(72 reductions, 95 cells)
This exercise show how many values in a list are verified by p condition.
Thanks
This is not tail recursive because of
Tail calls should return the result of the recursive call, rather than doing calculation with the result of the recursive call.
You can convert a non-tail recursive function to be tail-recursive by using an accumulator where you perform the additional work, i.e.
Say you have a simple counting function
You can make it tail recursive like so: