tail recursion
play

Tail Recursion Dr. Mattox Beckman University of Illinois at - PowerPoint PPT Presentation

Objectives Accumulating Recursion Tail Recursion Dr. Mattox Beckman University of Illinois at Urbana-Champaign Department of Computer Science . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


  1. Objectives Accumulating Recursion Tail Recursion Dr. Mattox Beckman University of Illinois at Urbana-Champaign Department of Computer Science . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

  2. Objectives Accumulating Recursion Objectives ▶ Understand what makes a function tail recursive. ▶ Explain how the compiler makes tail recursion efficient. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

  3. 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

  4. | 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) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

  5. 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 ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

  6. 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 ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

  7. 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 ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

  8. 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 ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

  9. 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 ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

  10. 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 ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

  11. 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 ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

  12. 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 ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

  13. 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 ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

  14. 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... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

  15. 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... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

  16. 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... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

  17. 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... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

  18. 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... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

  19. 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... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

  20. 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. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

  21. 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

  22. 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

  23. 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

  24. 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

  25. 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

  26. 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. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend