Competency 09–1:
Recursive Python Functions

Identifier: PyRec

Prerequisites: TUR, LOOP, LIST, SiffListBuild

Transition from Sifflet to Python

Learning Objectives

Students should be able to:

  1. Convert Sifflet functions to Python functions (i.e., write equivalent Python function definitions).
  2. Trace the calls of recursive functions in Python.

Lesson and Practice (Combined)

Reading

Textbook Chapter 9, pages 305–310.

Lecture and Practice

Recursive functions can be written in Python, or for that matter, most other programming languages, as well as Sifflet.

Practice 1

Study one or more of the Sifflet examples fact, sumFromZero. Define equivalent Python functions; i.e., translate the definitions into Python.

Lists

Python does not have built-in equivalents of the Sifflet list functions :, null, head, tail; therefore, we must define them.

Practice 2

Study one or more of the Sifflet examples length and sum (of a list). Translate their definitions into Python.

Tracing

To trace a function means to show how it computes its result; in the case of a recursive function, this involves showing the actual parameters and return values for each call of the function.

Practice 3

Trace the Python versions of one of the functions from Practice 1 and one of the functions from Practice 2.