Objectives Accumulating Recursion Tail Recursion Dr. Mattox Beckman University of Illinois at Urbana-Champaign Department of Computer Science . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Objectives Accumulating Recursion Objectives ▶ Understand what makes a function tail recursive. ▶ Explain how the compiler makes tail recursion efficient. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Objectives Accumulating Recursion Tail Calls Tail Position A subexpression s of expressions e , if it is evaluated, will be taken as the value of e . ▶ if x > 3 then x + 2 else x - 4 ▶ f (x * 3) — no (proper) tail position here. Tail Call A function call that occurs in tail position. ▶ if h x then h x else x + g x . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
| odd n = calc (n*3+1) (i+1) | otherwise = calc (n `div` 2) (i+1) Objectives Accumulating Recursion Your Turn Find the tail calls! Example Code 1 calc n i | n==2 = i 2 3 4 5 fib 0 = 0 6 fib 1 = 1 7 fib n = fib (n-1) + fib (n-2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Objectives Accumulating Recursion Tail Call Example ▶ If one function calls another in tail position, we get a special behavior. Example 1 foo x = bar (x+1) 2 bar y = baz (y+1) 3 baz z = z * 10 ▶ What happens when we call foo 1 ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ret 1 x Objectives Accumulating Recursion Tail Call Example ▶ If one function calls another in tail position, we get a special behavior. Example 1 foo x = bar (x+1) 2 bar y = baz (y+1) 3 baz z = z * 10 ▶ What happens when we call foo 1 ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ret 2 y ret 1 x Objectives Accumulating Recursion Tail Call Example ▶ If one function calls another in tail position, we get a special behavior. Example 1 foo x = bar (x+1) 2 bar y = baz (y+1) 3 baz z = z * 10 ▶ What happens when we call foo 1 ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ret 2 1 ret y x ret z 3 Objectives Accumulating Recursion Tail Call Example ▶ If one function calls another in tail position, we get a special behavior. Example 1 foo x = bar (x+1) 2 bar y = baz (y+1) 3 baz z = z * 10 ▶ What happens when we call foo 1 ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
30 ret 1 ret y 2 x z 3 ret Objectives Accumulating Recursion Tail Call Example ▶ If one function calls another in tail position, we get a special behavior. Example 1 foo x = bar (x+1) 2 bar y = baz (y+1) 3 baz z = z * 10 ▶ What happens when we call foo 1 ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
30 ret 1 ret y 2 30 x 3 z ret Objectives Accumulating Recursion Tail Call Example ▶ If one function calls another in tail position, we get a special behavior. Example 1 foo x = bar (x+1) 2 bar y = baz (y+1) 3 baz z = z * 10 ▶ What happens when we call foo 1 ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
30 ret x 1 y 2 30 ret 30 z 3 ret Objectives Accumulating Recursion Tail Call Example ▶ If one function calls another in tail position, we get a special behavior. Example 1 foo x = bar (x+1) 2 bar y = baz (y+1) 3 baz z = z * 10 ▶ What happens when we call foo 1 ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
30 2 ret x 3 1 z 30 30 ret ret 30 y Objectives Accumulating Recursion Tail Call Example ▶ If one function calls another in tail position, we get a special behavior. Example 1 foo x = bar (x+1) 2 bar y = baz (y+1) 3 baz z = z * 10 ▶ What happens when we call foo 1 ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
30 ret x 1 ret 30 y 2 30 30 3 30 ret z Objectives Accumulating Recursion Tail Call Example ▶ If one function calls another in tail position, we get a special behavior. Example 1 foo x = bar (x+1) 2 bar y = baz (y+1) 3 baz z = z * 10 ▶ What happens when we call foo 1 ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Objectives Accumulating Recursion The Tail Call Optimization Example 1 foo x = bar (x+1) 2 bar y = baz (y+1) 3 baz z = z * 10 ▶ If that’s the case, we can cut out the middle man... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ret 1 x Objectives Accumulating Recursion The Tail Call Optimization Example 1 foo x = bar (x+1) 2 bar y = baz (y+1) 3 baz z = z * 10 ▶ If that’s the case, we can cut out the middle man... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ret ret 2 x y 1 Objectives Accumulating Recursion The Tail Call Optimization Example 1 foo x = bar (x+1) 2 bar y = baz (y+1) 3 baz z = z * 10 ▶ If that’s the case, we can cut out the middle man... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ret x 3 z ret 2 y ret 1 Objectives Accumulating Recursion The Tail Call Optimization Example 1 foo x = bar (x+1) 2 bar y = baz (y+1) 3 baz z = z * 10 ▶ If that’s the case, we can cut out the middle man... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
30 ret x ret y 2 1 3 ret z Objectives Accumulating Recursion The Tail Call Optimization Example 1 foo x = bar (x+1) 2 bar y = baz (y+1) 3 baz z = z * 10 ▶ If that’s the case, we can cut out the middle man... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
30 2 x 30 ret 30 y 1 ret z 3 ret Objectives Accumulating Recursion The Tail Call Optimization Example 1 foo x = bar (x+1) 2 bar y = baz (y+1) 3 baz z = z * 10 ▶ If that’s the case, we can cut out the middle man... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Objectives Accumulating Recursion The Tail Call Optimization Example 1 foo x = bar (x+1) 2 bar y = baz (y+1) 3 baz z = z * 10 ▶ If that’s the case, we can cut out the middle man... ▶ Actually, we can do even better than that. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Objectives Accumulating Recursion The optimization ▶ When a function is in tail position, the compiler will recycle the activation record ! Example 1 foo x = bar (x+1) 2 bar y = baz (y+1) 3 baz z = z * 10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ret 1 x Objectives Accumulating Recursion The optimization ▶ When a function is in tail position, the compiler will recycle the activation record ! Example 1 foo x = bar (x+1) 2 bar y = baz (y+1) 3 baz z = z * 10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ret 2 y Objectives Accumulating Recursion The optimization ▶ When a function is in tail position, the compiler will recycle the activation record ! Example 1 foo x = bar (x+1) 2 bar y = baz (y+1) 3 baz z = z * 10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ret 3 z Objectives Accumulating Recursion The optimization ▶ When a function is in tail position, the compiler will recycle the activation record ! Example 1 foo x = bar (x+1) 2 bar y = baz (y+1) 3 baz z = z * 10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
30 ret 3 z Objectives Accumulating Recursion The optimization ▶ When a function is in tail position, the compiler will recycle the activation record ! Example 1 foo x = bar (x+1) 2 bar y = baz (y+1) 3 baz z = z * 10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
30 ret 3 z Objectives Accumulating Recursion The optimization ▶ When a function is in tail position, the compiler will recycle the activation record ! Example 1 foo x = bar (x+1) 2 bar y = baz (y+1) 3 baz z = z * 10 ▶ This allows recursive functions to be written as loops internally. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Recommend
More recommend