CSC 151 - Pattern matching
Recall that one of our design goals is to write programs that are correct from inspection. In particular, when we have a recursive design, we want our code to look like that design. Let’s see how a possible definition of length fares in this respect. Below, we have replicated the definition of length with the recursive design in-lined in comments: Not too bad! Like our design, the code is clearly conditioned on whether lst is empty or non-empty. Furthermore, the results of the cases clearly implement the cases of our design, so we can believe our implementation is correct as long as we believe our design is correct. Is there anything we can improve here? Yes—some subtle, yet important things, in fact: To fix these issues, we’ll use the pattern matching facilities of Scamper to express our recursive design directly without the need for a guard expression or let-binding. A pattern match is a language construct that looks at and performs case analysis on the shape of a data type. First, w
CSC 151 - Pattern matching Pattern matching Due Friday, 10 October 2025 --> Summary We also introduce pattern matching as an elegant way of capturing case analysis on the shape of data. Recall that one of our design goals is to write programs that are correct from inspection. In particular, when we have a recursive design, we want our code to look like that design. Let’s see how a possible definition of length fares in this respect. Below, we have replicated the definition of length with the recursive design in-lined in comments: (define length (lambda (lst) (if (null? lst) ; A list is either
Explore this link on the map →saved by
related reading
- CSC 151 - Recursion Over Listseikmeier.sites.grinnell.edu
- CSC 151 - Recursion over Numberseikmeier.sites.grinnell.edu
- CSC 151 - Higher-order design, recursive and othereikmeier.sites.grinnell.edu
- CSC 151 - Tail Recursioneikmeier.sites.grinnell.edu
- CSC 151 - Analyzing procedureseikmeier.sites.grinnell.edu
- CSC 151 - List basicseikmeier.sites.grinnell.edu
- CSC 151 - Code Formatting and Styleeikmeier.sites.grinnell.edu
- CSC 151 - Coding Challenge 6eikmeier.sites.grinnell.edu
- CSC 151 - Coding Challenge 8eikmeier.sites.grinnell.edu
- CSC 151 - Coding Challenge 7eikmeier.sites.grinnell.edu
- CSC 151 - Naming values with local bindingseikmeier.sites.grinnell.edu
- CSC 151 - Coding Challenge 4eikmeier.sites.grinnell.edu