CSC 151 - Recursion Over Lists
As a first example of recursion, we designed a function that computes the sum of the elements of an input list. This program embodies the following decomposition of the sum problem: numbers is either an empty list or a non-empty list. And we derive this recursive decomposition from our recursive definition of a list: A list is either: In this sense, the recursive decomposition serves as a basic skeleton for how to design recursive functions over lists! In this reading, we’ll the practical considerations of applying this skeleton to decompose problems. Next, let’s take a look at another list problem—computing the length of a list—and see how we can systematically decompose the problem using our skeleton. Because our recursive definition of list is in terms of case, we expect that our decomposition of the problem should follow these cases. Thus, to compute the length of the list, we must consider what the length of a list is when: We’ll call the empty case the base case of our recursive
CSC 151 - Recursion Over Lists Recursion Over Lists Due Friday, 10 October 2025 --> A first example As a first example of recursion, we designed a function that computes the sum of the elements of an input list. (define sum (lambda (numbers) (if (null? numbers) 0 (+ (car numbers) (sum (cdr numbers)))))) (sum (list 91 85 96 82 89)) (sum (list -17 17 12 -4)) (sum (list 9.3)) (sum null) This program embodies the following decomposition of the sum problem: numbers is either an empty list or a non-empty list. The sum of an empty list is 0 . The sum of a non-empty list is the head of the list added
Explore this link on the map →saved by
related reading
- CSC 151 - Recursion over Numberseikmeier.sites.grinnell.edu
- CSC 151 - Pattern matchingeikmeier.sites.grinnell.edu
- CSC 151 - Higher-order design, recursive and othereikmeier.sites.grinnell.edu
- CSC 151 - Tail Recursioneikmeier.sites.grinnell.edu
- CSC 151 - Coding Challenge 6eikmeier.sites.grinnell.edu
- CSC 151 - Decompositioneikmeier.sites.grinnell.edu
- CSC 151 - Analyzing procedureseikmeier.sites.grinnell.edu
- CSC 151 - Transforming listseikmeier.sites.grinnell.edu
- CSC 151 - List basicseikmeier.sites.grinnell.edu
- CSC 151 - Coding Challenge 7eikmeier.sites.grinnell.edu
- CSC 151 - Coding Challenge 8eikmeier.sites.grinnell.edu
- CSC 151 - Coding Challenge 4eikmeier.sites.grinnell.edu