flâneur — a map of the web's best reading

CSC 151 - Pattern matching

eikmeier.sites.grinnell.edu · 1,878 words · saved by 1 readers

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