Let's say I've written a function in Haskell and I want to assert that it is tail-recursive and it will be optimized by the compiler. Is there a way to do it?
I know there's a way to do it in Scala with @tailrec annotation.
Example:
import scala.annotation.tailrec
class Factorial2 {
def factorial(n: Int): Int = {
@tailrec def factorialAcc(acc: Int, n: Int): Int = {
if (n <= 1) acc
else factorialAcc(n * acc, n - 1)
}
factorialAcc(1, n)
}
}
As for GHC 8.8.2 tail recursion cannot be forcefully asserted by any pragma or keyword.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With