Comp 283, 2025 Summer Session 1

Course Site for Comp 283

Recursion

There is still one type of proof we need to cover… but first we’re going to take a little bit of a detour.

It is helpful to understand recursion before we move on to the next kind of proof!

What is recursion?

Recursion is the idea of solving a problem by breaking it into smaller problems.

Example - Factorials:

Recursion in Coding

Here is an example of what recursion would look like in a programming language like Python.

Say we want to write a program that returns \(n!\).

def factorial(n):
    if n == 1: #base case
        return 1
    else: #recursive rule
        return n * factorial(n-1) 

Why do we use recursion?

Defining recursion

We just gave an example of a function that is defined recursively.

You can also recursively define sets, relations, tuples, lists, sequences, etc…

There are two main parts of any recursive definition:

  1. The base case: the initial element/value of what you’re trying to define
  2. The recursive rule: tells you how to generate the next element/value given previous ones.

Example: Factorials

  1. The base case: \(1! = 1\)
  2. The recursive rule: \(n! = n \cdot (n-1)!\)

The Fibonacci numbers

  1. The base case: \(F(0) = 0, F(1) = 1\)
  2. Recursive rule: For \(n > 1\), \(F(n) = F(n-1) + F(n-2)\).

Other Example - Powersets:

Definition

  1. The base case: \(\mathscr{P}(\{ \}) = \{\}\)
  2. The recursive rule: \(\mathscr{P}(X + \{c\}) = \mathscr{P}(X) \cup (\mathscr{P}(X) + \{c\})\)

Defining Lists Recursively

  1. Base case: Empty list \(\langle ~ \rangle\)
  2. Recursive rule: For any element \(a \in A\) and all lists \(l \in L\), \(cons(a,l)\) is a list in \(L\).

Defining Lists Recursively

  1. Base case: Empty list \(\langle ~ \rangle\)

  2. Recursive rule: For any element \(a \in A\) and all lists \(l \in L\), \(cons(a,l)\) is a list in \(L\).

Examples

Useful Partial Functions

Examples

Strings and Languages