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

basic recursion
SMART_READER_LITE
LIVE PREVIEW

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

Objectives Function Calls Example Recursions Lists References Basic Recursion Dr. Mattox Beckman University of Illinois at Urbana-Champaign Department of Computer Science Objectives Function Calls Example Recursions Lists References


slide-1
SLIDE 1

Objectives Function Calls Example Recursions Lists References

Basic Recursion

  • Dr. Mattox Beckman

University of Illinois at Urbana-Champaign Department of Computer Science

slide-2
SLIDE 2

Objectives Function Calls Example Recursions Lists References

Objectives

◮ Diagram the stack frames that result from a series of function calls. ◮ Use Haskell to write a recursive function on integers. ◮ Use Haskell to write a recursive function on lists.

slide-3
SLIDE 3

Objectives Function Calls Example Recursions Lists References

Function Calls

◮ Remember the syntax of a function defjnition in Haskell.

Function Syntax

1 foo a = 2

let aa = a * a

3

in aa + a

◮ The above function has one paramater and one local. ◮ If we call it three times, what will happen in memory?

1 x = (foo 1) + (foo 2) + (foo 3)

slide-4
SLIDE 4

Objectives Function Calls Example Recursions Lists References

Function Calls

◮ Remember the syntax of a function defjnition in Haskell.

Function Syntax

1 foo a = 2

let aa = a * a

3

in aa + a

◮ The above function has one paramater and one local. ◮ If we call it three times, what will happen in memory?

1 x = (foo 1) + (foo 2) + (foo 3)

First Call Second Call Third Call a 1 aa 1

slide-5
SLIDE 5

Objectives Function Calls Example Recursions Lists References

Function Calls

◮ Remember the syntax of a function defjnition in Haskell.

Function Syntax

1 foo a = 2

let aa = a * a

3

in aa + a

◮ The above function has one paramater and one local. ◮ If we call it three times, what will happen in memory?

1 x = (foo 1) + (foo 2) + (foo 3)

First Call Second Call Third Call a 1 aa 1 a 2 aa 4

slide-6
SLIDE 6

Objectives Function Calls Example Recursions Lists References

Function Calls

◮ Remember the syntax of a function defjnition in Haskell.

Function Syntax

1 foo a = 2

let aa = a * a

3

in aa + a

◮ The above function has one paramater and one local. ◮ If we call it three times, what will happen in memory?

1 x = (foo 1) + (foo 2) + (foo 3)

First Call Second Call Third Call a 1 aa 1 a 2 aa 4 a 3 aa 9

slide-7
SLIDE 7

Objectives Function Calls Example Recursions Lists References

Functions Calling Functions

◮ If one function calls another, both activation records exist simultaneously.

1 foo x = x + bar (x+1) 2 bar y = y + baz (y+1) 3 baz z = z * 10

◮ What happens when we call foo 1?

slide-8
SLIDE 8

Objectives Function Calls Example Recursions Lists References

Functions Calling Functions

◮ If one function calls another, both activation records exist simultaneously.

1 foo x = x + bar (x+1) 2 bar y = y + baz (y+1) 3 baz z = z * 10

◮ What happens when we call foo 1?

x 1 ret

slide-9
SLIDE 9

Objectives Function Calls Example Recursions Lists References

Functions Calling Functions

◮ If one function calls another, both activation records exist simultaneously.

1 foo x = x + bar (x+1) 2 bar y = y + baz (y+1) 3 baz z = z * 10

◮ What happens when we call foo 1?

x 1 ret y 2 ret

slide-10
SLIDE 10

Objectives Function Calls Example Recursions Lists References

Functions Calling Functions

◮ If one function calls another, both activation records exist simultaneously.

1 foo x = x + bar (x+1) 2 bar y = y + baz (y+1) 3 baz z = z * 10

◮ What happens when we call foo 1?

x 1 ret y 2 ret z 3 ret

slide-11
SLIDE 11

Objectives Function Calls Example Recursions Lists References

Functions Calling Functions

◮ If one function calls another, both activation records exist simultaneously.

1 foo x = x + bar (x+1) 2 bar y = y + baz (y+1) 3 baz z = z * 10

◮ What happens when we call foo 1?

x 1 ret y 2 ret z 3 ret 30

slide-12
SLIDE 12

Objectives Function Calls Example Recursions Lists References

Functions Calling Functions

◮ If one function calls another, both activation records exist simultaneously.

1 foo x = x + bar (x+1) 2 bar y = y + baz (y+1) 3 baz z = z * 10

◮ What happens when we call foo 1?

x 1 ret y 2 30 ret z 3 ret 30

slide-13
SLIDE 13

Objectives Function Calls Example Recursions Lists References

Functions Calling Functions

◮ If one function calls another, both activation records exist simultaneously.

1 foo x = x + bar (x+1) 2 bar y = y + baz (y+1) 3 baz z = z * 10

◮ What happens when we call foo 1?

x 1 ret y 2 30 ret 32 z 3 ret 30

slide-14
SLIDE 14

Objectives Function Calls Example Recursions Lists References

Functions Calling Functions

◮ If one function calls another, both activation records exist simultaneously.

1 foo x = x + bar (x+1) 2 bar y = y + baz (y+1) 3 baz z = z * 10

◮ What happens when we call foo 1?

x 1 32 ret y 2 30 ret 32 z 3 ret 30

slide-15
SLIDE 15

Objectives Function Calls Example Recursions Lists References

Functions Calling Functions

◮ If one function calls another, both activation records exist simultaneously.

1 foo x = x + bar (x+1) 2 bar y = y + baz (y+1) 3 baz z = z * 10

◮ What happens when we call foo 1?

x 1 32 ret 33 y 2 30 ret 32 z 3 ret 30

slide-16
SLIDE 16

Objectives Function Calls Example Recursions Lists References

Factorial

◮ This works if the function calls itself.

Factorial

1 fact 0 = 1 2 fact 1 = 1 3 fact n = n * fact (n-1)

◮ fact 4 ...

n 4 6 ret 24 n 3 2 ret 6 n 2 1 ret 2 n 1 ret 1

slide-17
SLIDE 17

Objectives Function Calls Example Recursions Lists References

Lists in Haskell

◮ Haskell has a built-in syntax for singly linked lists. ◮ The empty list is []. ◮ You can use : to create a new list …

1 : 2 : 3 : 4 : [] 1 2 3 4

◮ You can also write [1,2,3,4].

slide-18
SLIDE 18

Objectives Function Calls Example Recursions Lists References

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.

slide-19
SLIDE 19

Objectives Function Calls Example Recursions Lists References

Activity

◮ Write a function fib that computes the nth Fibonacci number Fn. Let F1 = 1 and F2 = 1. ◮ Write a function sumList that takes a list and sums its elements. ◮ Write a function incList that takes a list and increments its elements.

slide-20
SLIDE 20

Objectives Function Calls Example Recursions Lists References

Solutions to fib and sumList

1 fib 1 = 1 2 fib 2 = 1 3 fib n = fib (n-1) + fib (n-2) 4 5 sumList [] = 0 6 sumList (x:xs) = x + sumList xs

slide-21
SLIDE 21

Objectives Function Calls Example Recursions Lists References

Solution to incList

◮ Remember that you must create a new list!

1 incList [] = [] 2 incList (x:xs) = x+1 : incList xs

slide-22
SLIDE 22

Objectives Function Calls Example Recursions Lists References

History

◮ The fjrst programming language to implement recursion was Lisp in 1958. [McC79]

References [McC79] John McCarthy. History of Lisp. Stanford University, 1979. URL: http://www-formal.stanford.edu/jmc/history/lisp/lisp.html.