Tail Recursion: Conceptual
IDentifying tail recursion
For each function identify if it is tail recursive or not and explain why.
(define (f1 x)
(if (= x 0)
1
(if (= (remainder x 2) 0)
(+ 1 (f1 (- x 1)))
(+ x (f1 (- x 1))))))
(define (f2 x)
(define (helper y z)
(if (= y 0)
z
(helper (- y 1) (+ z y))))
(helper x 0))
(define (f3 x):
(if (= x 0)
0
(f1 x)))
Creating a tail recursive function
Turn the summer function into a tail recursive function. Hint: use a helper function.
(define (summer num)
(if (= x 0)
0
(+ num (summer (- num 1)))))
code Writing
Write a tail recursive function that takes in a list and reverses it.
> (define lst '(1 2 3 4 5))
> (reverse lst)
(5 4 3 2 1)