;;; N-to-the-K ;;; ---------------------------------------- ;;; INPUTS: N, K, non-negative integers (not both 0) ;;; OUTPUT: the value of N to the Kth power (define n-to-the-k (lambda (n k) ;; Call the accumulator-based tail-recursive helper function ;; with an appropriately initialized accumulator (n-to-the-k-acc n k 1))) ;;; N-to-the-K-acc ;;; ---------------------------------------- ;;; INPUTS: N, K, as above ;;; ACC, a multiplicative accumulator ;;; ---------------------------------------- ;;; Can use K as a counter (the number of times N is multiplied) (define n-to-the-k-acc (lambda (n k acc) (cond ;; Base Case: K = 0 ((<= k 0) ;; return the accumulator acc) ;; TAIL-Recursive Case: K > 0 (#t ;; tail-recursive function call with updated inputs (n-to-the-k-acc n (- k 1) (* acc n)))))) ;;; N-To-the_k ;;; ---------------------------- (define n-to-the-k-alt (lambda (n k) (local [(define acc 1)] (dotimes (i k) (set! acc (* acc n))) ;; return the value of the accumulator acc)))