6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 5.3.4 Grouping together larger collections Now, suppose we decide that we want to take a group of points Suppose we want to group together a set of points. Here is(which might have segments defined between adjacent points one way (cons (cons (cons (cons and manipulate these groups of points. For example, a figure m might be defined as a group of ordered points, with segments (cons between each consecutive pair of points. And we might want to (cons p7 P8))) stretch that whole group, or rotate it, or do something else to it UGHl! How do we get out the parts to manipulate them? How do we group these things together Well, one possibility is just to use a bunch of cons pairs, such as shown here. But while this is a perfectly reasonable way to glue things together, it is going to be a bear to manipulate. Suppose 0O1 sIcP I we want to stretch all these points? We would have to write code that would put together the right collections of and cdrs to get out the pieces, perform a computation on them, and then glue them back together again. This will be a royal pain! It would be better if we had a more convenient and conventional way of gluing together groups of things and fortunately we do Slide 5.3.5 Conventional interfaces -lists Pairs are a nice way of gluing two things together. However, A list is a data object that can hold an arbitrary number of sometimes I may want the ability to glue together arbitrary ordered items numbers of things, and here pairs are less helpful. Fortunately, Scheme also has a primitive way of gluing together arbitrary sets of objects, called a list, which is a data object with an arbitrary number of ordered elements within it Conventional interfaces - lists Slide 5.3.6 Of course, we could make a list by just consing together a set of A list is a data object that can hold an arbitrary number of things, using however many pairs we need. But it is much more More formally, a list is a se convenient to think of a list as a basic structure. and here is more formally how we define such a structure. A list is a sequence of Cdr-part of a pair in sequence- holds a pointer to rest pairs, with the following properties. The car part of a pair in the Empty-ist ni1-signals no more pairs, or end of list list holds the next element of the list The cdr part of a pair in the list holds a pointer to the rest of the list We will also need to tell when we are at the end of the list, and we have a special symbol nil, that signals the fact that there are no more pairs in the list6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 5.3.4 Now, suppose we decide that we want to take a group of points (which might have segments defined between adjacent points) and manipulate these groups of points. For example, a figure might be defined as a group of ordered points, with segments between each consecutive pair of points. And we might want to stretch that whole group, or rotate it, or do something else to it. How do we group these things together? Well, one possibility is just to use a bunch of cons pairs, such as shown here. But while this is a perfectly reasonable way to glue things together, it is going to be a bear to manipulate. Suppose we want to stretch all these points? We would have to write code that would put together the right collections of car’s and cdr’s to get out the pieces, perform a computation on them, and then glue them back together again. This will be a royal pain! It would be better if we had a more convenient and conventional way of gluing together groups of things, and fortunately we do. Slide 5.3.5 Pairs are a nice way of gluing two things together. However, sometimes I may want the ability to glue together arbitrary numbers of things, and here pairs are less helpful. Fortunately, Scheme also has a primitive way of gluing together arbitrary sets of objects, called a list, which is a data object with an arbitrary number of ordered elements within it. Slide 5.3.6 Of course, we could make a list by just consing together a set of things, using however many pairs we need. But it is much more convenient to think of a list as a basic structure, and here is more formally how we define such a structure. A list is a sequence of pairs, with the following properties. The car part of a pair in the list holds the next element of the list. The cdr part of a pair in the list holds a pointer to the rest of the list. We will also need to tell when we are at the end of the list, and we have a special symbol, nil, that signals the fact that there are no more pairs in the list