Download CS421 Lecture 3: Recursion Patterns - Lists, Complexity, and Accumulating - Prof. Elsa Gun and more Study notes Computer Science in PDF only on Docsity!
Objectives and Introduction Recursion and Lists Complexity Accumulating Recursion Activities
CS421 Lecture 3: Patterns of Recursion
Mark Hills
University of Illinois at Urbana-Champaign
June 3, 2008
Based on slides by Mattox Beckman, as updated by Vikram Adve, Gul
Agha, and Elsa Gunter
Objectives and Introduction Recursion and Lists Complexity Accumulating Recursion Activities
(^1) Objectives and Introduction
(^2) Recursion and Lists
(^3) Complexity
(^4) Accumulating Recursion
(^5) Activities
Objectives and Introduction Recursion and Lists Complexity Accumulating Recursion Activities
Recursion Example
Compute n
recursively using formula: n
= (2n − 1) + (n − 1)
1 # let rec nthsq n = (* note rec keyword *)
2 match n with (* pattern matching *)
3 | 0 -> 0 (* base case *)
4 | n -> (2n - 1) ( recursive case *)
5 + nthsq (n-1);; (* recursive call *)
6 val nthsq : int -> int =
7 # nthsq 3;;
8 - : int = 9
Recursive functions must be declared with a rec keyword.
Structure of a recursive function is similar to
proof-by-induction
Objectives and Introduction Recursion and Lists Complexity Accumulating Recursion Activities
Recursion and Induction
1 # let rec nthsq n = match n with
3 | n -> (2*n-1) + (nthsq (n-1));;
The base case is the last case: it stops the computation.
The recursive case calls the same function with a smaller
argument (by some measure) than the current call
Key: Must take progress towards base case
The if or match statement has to be able to tell when the
base case is reached.
Failure to do any of the above will cause failure to terminate.
Objectives and Introduction Recursion and Lists Complexity Accumulating Recursion Activities
Basic List Recursion Forward Recursion Mapping Recursion Folding Recursion
Lists
1 # let rec length lst = match lst with
2 | [] -> 0
3 | x::xs -> 1 + length xs;;
4 val length : ’a list -> int =
5 # length [2;3;4;6];;
6 - : int = 4
The base case [] stops the computation.
Your recursive case calls itself with a smaller argument (xs)
than the original call
Objectives and Introduction Recursion and Lists Complexity Accumulating Recursion Activities
Basic List Recursion Forward Recursion Mapping Recursion Folding Recursion
Activity 1
(108) Write an OCaml function that returns the maximum element
of a list. (Assume the list always has at least 1 element, and
assume you have a max function.)
Objectives and Introduction Recursion and Lists Complexity Accumulating Recursion Activities
Basic List Recursion Forward Recursion Mapping Recursion Folding Recursion
Mapping Recursion
One common form maps a function to each of the elements.
1 # let rec doubleList lst = match lst with
2 | [] -> []
3 | x::xs -> 2 * x :: doubleList xs;;
4 val doubleList : int list -> int list =
5 # doubleList [4;6;8];;
6 - : int list = [8; 12; 16]
Objectives and Introduction Recursion and Lists Complexity Accumulating Recursion Activities
Basic List Recursion Forward Recursion Mapping Recursion Folding Recursion
Generic Maps
Map is a general enough operation to be defined for all types of
lists.
1 # let rec map f l =
2 match l with
3 [] -> []
4 | (x :: xs) -> (f x) :: (map f xs) ;;
5 val map : (’a -> ’b) -> ’a list -> ’b list =
6 # let double n = 2 * n ;;
7 val double : int -> int =
8 # map double [1;2;3;4;5];;
9 - : int list = [2; 4; 6; 8; 10]
Objectives and Introduction Recursion and Lists Complexity Accumulating Recursion Activities
Basic List Recursion Forward Recursion Mapping Recursion Folding Recursion
Folding Recursion
Another common form “folds” a list via some function.
1 # let rec multList lst = match lst with
2 | [] -> 1
3 | x::xs -> x * multList xs;;
4 val multList : int list -> int =
5 # multList [2;4;6];;
6 - : int = 48
This computes (2 ∗ (4 ∗ (6 ∗ 1))).
Objectives and Introduction Recursion and Lists Complexity Accumulating Recursion Activities
Basic List Recursion Forward Recursion Mapping Recursion Folding Recursion
Generic Folds: Fold Left
Like map, fold is also generic, but requires an additional piece of
information – an identity.
1 # let rec fold_left f i l =
2 match l with
3 [] -> i
4 | (x :: xs) -> fold_left f (f i x) xs;;
5 val fold_left :
6 (’a -> ’b -> ’a) -> ’a -> ’b list -> ’a =
7 # fold_left ( * ) 1 [2;4;6] ;;
8 - : int = 48
Objectives and Introduction Recursion and Lists Complexity Accumulating Recursion Activities
Basic List Recursion Forward Recursion Mapping Recursion Folding Recursion
List.folds
Folds are also defined in the standard libraries.
1 # List.fold_left ( * ) 1 [2;4;6] ;;
2 - : int = 48
3 # List.fold_right ( * ) [2;4;6] 1 ;;
4 - : int = 48
Objectives and Introduction Recursion and Lists Complexity Accumulating Recursion Activities
Overview Linear Time Quadratic Time Exponential Time
How long will it take?
For an input of size n,
how long will it take to generate the output?
Common big-O times for recursive computations:
Constant time O(1) — input size doesn’t matter
Linear time O(n) — double input ⇒ double time
Quadratic time O(n
) — double input ⇒ quadruple time
Exponential time O(
n
) — increment input ⇒ double time
Review big-O notation from CS 225.
Objectives and Introduction Recursion and Lists Complexity Accumulating Recursion Activities
Overview Linear Time Quadratic Time Exponential Time
Quadratic Time
Each step of the recursion takes time proportional to the
input.
Each step of the recursion makes exactly one recursive call.
List example: badly written “reverse”.
Integer example: Twelve days of Christmas.
1 # let rec badReverse lst = match lst with
2 | [] -> []
3 | x::xs -> (badReverse xs) @ [x];;
4 val badReverse : ’a list -> ’a list =
Objectives and Introduction Recursion and Lists Complexity Accumulating Recursion Activities
Overview Linear Time Quadratic Time Exponential Time
Exponential Time
Hideous running times.
Each step of the recursion takes constant time.
But each recursion makes two recursive calls.
Worst part: it is very simple to make a linear function into an
exponential one!
Examples: n¨aive Fibonacci sequence (1,1,2,3,5,8,13,21,34... )
1 # let rec badFib n = match n with
4 | _ -> badFib (n-1) + badFib (n-2);;
5 val badFib : int -> int =