Question: ( 3 . 1 . 4 ) An algorithm is said to be tail recursive when all of its recursive calls are in tail position
An algorithm is said to be tail recursive when all of its recursive calls are in tail position: when each recursive call is the final operation that the algorithm must perform before returning. Such algorithms can often be more aggressively optimized into more efficient machine code. Consider the following recursive algorithm: MaxDifferenceA a a an Input: A finite sequence A of n integers Output: The largest difference between consecutive integers in A : if n then : return a a: else : return max a a MaxDifferencea a an This algorithm finds the largest difference between consecutive elements in a sequence. For example, given A it returns However, it is not tail recursive, because it must perform the max a a operations after its recursive call. Rewrite this algorithm tail recursively and also nongeneratively
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
