So I tried to write a tail-recursive exponentiation (for positive integer exponents). I came with this (it's exponentiation by squaring):
(define (^ n k)
(define (helper n k acc)
(cond
[(zero? k) acc]
[(even? k) (helper (* n n) (/ k 2) acc)]
[else (helper n (- k 1) (* n acc))]))
(helper n k 1))
If however I try to run it with big argument, I always get to Racket virtual machine has run out of memory; aborting
. Why this function is not considered tail-recursive? Every condition leads straight to either recursive execution, or terminal symbol.