Write a function that accepts n and returns the size of the nth triangular number. Oh no! by-induction is not tail recursive. But that's nbd given by-geometry.
$ cat tns.rkt #lang racket (define (by-geometry n) ; Return the size of the nth triangular number. ; Take two nth triangular numbers and place them ; side-by-side, apex to base. The result is a ; parallelogram of height n and width n + 1. One of the ; triangles is half the size of the parallelogram. (/ (* (+ n 1) n) 2)) (define (by-induction n) ; Return the size of the nth triangular number. ; The nth triangular number is n larger than the (n - 1)th ; triangular number. The 0th triangular number has size ; 0. (if (< n 1) 0 (+ n (by-induction (- n 1))))) (require rackunit) (do ((n 0 (+ n 1))) ((> n 101)) (check-eq? (by-geometry n) (by-induction n))) (check-eq? (by-geometry 63) 2016) $ mzscheme tns.rkt $