objectives basic recursion
play

Objectives Basic Recursion Your goal for this lecture is to - PowerPoint PPT Presentation

1 in aa + a aa 1 a in aa + a let aa = a * a let aa = a * a Objectives Function Calls Iterative Recursion Recursion and Lists Objectives Function Calls Iterative Recursion Recursion and Lists Objectives Basic Recursion Your goal for


  1. 1 in aa + a aa 1 a in aa + a let aa = a * a let aa = a * a Objectives Function Calls Iterative Recursion Recursion and Lists Objectives Function Calls Iterative Recursion Recursion and Lists Objectives Basic Recursion Your goal for this lecture is to understand recursion — at least, to get a start on it. We will talk about Dr. Mattox Beckman ◮ Diagram a series of function calls. ◮ Show how to write a recursive function on integers. University of Illinois at Urbana-Champaign Department of Computer Science ◮ Show how to write a recursive function on lists. Objectives Function Calls Iterative Recursion Recursion and Lists Objectives Function Calls Iterative Recursion Recursion and Lists Function Calls Function Calls ◮ Remember the syntax of a function definition in Haskell. Function Syntax ◮ Remember the syntax of a function definition in Haskell. 1 foo a = Function Syntax 2 1 foo a = 3 ◮ The above function has one paramater and one local. 2 3 ◮ If we call it three times, what will happen in memory? ◮ The above function has one paramater and one local. 1 x = (foo 1) + (foo 2) + (foo 3) ◮ If we call it three times, what will happen in memory? First Call Second Call Third Call 1 x = (foo 1) + (foo 2) + (foo 3)

  2. ret 1 4 aa 2 a 1 aa 1 a a 1 aa a let aa = a * a 2 in aa + a aa let aa = a * a 4 a 3 aa 9 x 1 in aa + a Objectives Function Calls Iterative Recursion Recursion and Lists Objectives Function Calls Iterative Recursion Recursion and Lists Function Calls Function Calls ◮ Remember the syntax of a function definition in Haskell. ◮ Remember the syntax of a function definition in Haskell. Function Syntax Function Syntax 1 foo a = 1 foo a = 2 2 3 3 ◮ The above function has one paramater and one local. ◮ The above function has one paramater and one local. ◮ If we call it three times, what will happen in memory? ◮ If we call it three times, what will happen in memory? 1 x = (foo 1) + (foo 2) + (foo 3) 1 x = (foo 1) + (foo 2) + (foo 3) First Call Second Call Third Call First Call Second Call Third Call Objectives Function Calls Iterative Recursion Recursion and Lists Objectives Function Calls Iterative Recursion Recursion and Lists Functions Calling Functions Functions Calling Functions ◮ If one function calls another, both activation records exist simultaneously. ◮ If one function calls another, both activation records exist 1 foo x = x + bar (x + 1) simultaneously. 2 bar y = y + baz (y + 1) 3 baz z = z * 10 1 foo x = x + bar (x + 1) ◮ What happens when we call foo 1 ? 2 bar y = y + baz (y + 1) 3 baz z = z * 10 ◮ What happens when we call foo 1 ?

  3. 30 x z ret 2 y ret 1 x ret 1 ret y 2 ret z 3 30 ret y ret 3 z ret 30 2 ret x 1 x 1 ret y 2 ret 3 Objectives Function Calls Iterative Recursion Recursion and Lists Objectives Function Calls Iterative Recursion Recursion and Lists Functions Calling Functions Functions Calling Functions ◮ If one function calls another, both activation records exist ◮ If one function calls another, both activation records exist simultaneously. simultaneously. 1 foo x = x + bar (x + 1) 1 foo x = x + bar (x + 1) 2 bar y = y + baz (y + 1) 2 bar y = y + baz (y + 1) 3 baz z = z * 10 3 baz z = z * 10 ◮ What happens when we call foo 1 ? ◮ What happens when we call foo 1 ? Objectives Function Calls Iterative Recursion Recursion and Lists Objectives Function Calls Iterative Recursion Recursion and Lists Functions Calling Functions Functions Calling Functions ◮ If one function calls another, both activation records exist ◮ If one function calls another, both activation records exist simultaneously. simultaneously. 1 foo x = x + bar (x + 1) 1 foo x = x + bar (x + 1) 2 bar y = y + baz (y + 1) 2 bar y = y + baz (y + 1) 3 baz z = z * 10 3 baz z = z * 10 ◮ What happens when we call foo 1 ? ◮ What happens when we call foo 1 ?

  4. 1 z 30 ret x 1 32 ret y 2 30 ret 32 3 4 ret 30 3 z 32 ret 30 2 y 33 ret 32 n 6 x y ret 1 n 2 ret 1 2 n 6 x 1 ret 2 ret 30 ret 32 z 3 ret 30 ret 2 3 n 24 1 Objectives Function Calls Iterative Recursion Recursion and Lists Objectives Function Calls Iterative Recursion Recursion and Lists Functions Calling Functions Functions Calling Functions ◮ If one function calls another, both activation records exist ◮ If one function calls another, both activation records exist simultaneously. simultaneously. 1 foo x = x + bar (x + 1) 1 foo x = x + bar (x + 1) 2 bar y = y + baz (y + 1) 2 bar y = y + baz (y + 1) 3 baz z = z * 10 3 baz z = z * 10 ◮ What happens when we call foo 1 ? ◮ What happens when we call foo 1 ? Objectives Function Calls Iterative Recursion Recursion and Lists Objectives Function Calls Iterative Recursion Recursion and Lists Functions Calling Functions Factorial ◮ This works if the function calls itself. ◮ If one function calls another, both activation records exist Factorial simultaneously. 1 fact 0 = 1 1 foo x = x + bar (x + 1) 2 fact 1 = 1 2 bar y = y + baz (y + 1) 3 fact n = n * fact (n - 1) 3 baz z = z * 10 ◮ fact 4 ... ◮ What happens when we call foo 1 ?

  5. Objectives Function Calls Iterative Recursion Recursion and Lists Lists Because lists are recursive, functions that deal with lists tend to be recursive. Length 1 mylength :: [a] -> Int 2 mylength [] = 0 3 mylength (x : xs) = 1 + mylength xs 4 5 mylength s -- would return 3 ◮ The base case stops the computation. ◮ Your recursive case calls itself with a smaller argument than the original call.

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