正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 30.1.13 Remember that there are several important properties to pairs Properties of data structures and lists. They have a contract between constructor and Contract between constructor and selectors selectors. They have the property of closure that is, that the Property of closure result of creating an instance of an object can itself be used to A list is a sequence of pairs, ending in the create a new object empty list, nil This is worth exploring a bit more carefully. So what is a list? It Consing anything onto a list results in a list(by is a sequence of pairs, ending in the special symbol nil, or definition) empty list. Thus, consing anything onto a list gives you a new Taking the cdr of a list results in a list(except perhaps for the empty list) sequence of pairs, ending in the empty list, hence a list Would be better to use adjoin, first and rest, Similarly, taking the cdr of a sequence of pairs ending in the instead of cons car and cdr. empty list results in a shorter sequence of pairs ending in the 4 &2003 6001S empty list, and hence is a list. Thus lists are closed under the operations of cons and cdr Note that this is not quite right. What happens if you try to take the cdr of nil? In MIT Scheme you get an error, but by this definition it would be better to just return the empty list Also notice that it would really be better to have distinctive operations for lists, as compared to pairs, e.g. adjoin first and rest, instead of cons, car and cdr, to distinguish operations on lists from operations on pairs. For historical reasons, we stick with the latter, even though the better would be conceptually much cleaner Slide 30.1.14 Completing our abstraction So let's use this to build a specific version of our abstraction Note how our abstraction nicely inherits its contract from the Points or vectors: underlying contract for pairs and lists. And note how points create a nested structure underneath lines. That is a line (define yor cdr] segment is a list of two elements, each of which points to Line segments: 山啁 another abstraction, namely a pair representing a vector (define makesegment list) (define start-segment car) 6 001 SICP Slide 30.1.15 Okay, now let's put the pieces together. George is just defined Drawing in a rectangle or frame as a collection(a list in this case)of line segments. All we need to do is take a rectangle or frame(which is just a pair of orthogonal line segments), and draw these segments within that rectangle. But as we start thinking towards the kinds of operations we did on George earlier. we would like to be able to draw george in different frames. So we would like to be able to define any frame. either of different size. or even non-orthogonal. and draw George inside of it. What does that mean? Ideally, we could take George( defined as a set of segments within a frame) and stretch those segments to fit within a new frame6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 30.1.13 Remember that there are several important properties to pairs and lists. They have a contract between constructor and selectors. They have the property of closure, that is, that the result of creating an instance of an object can itself be used to create a new object. This is worth exploring a bit more carefully. So what is a list? It is a sequence of pairs, ending in the special symbol nil, or empty list. Thus, consing anything onto a list gives you a new sequence of pairs, ending in the empty list, hence a list. Similarly, taking the cdr of a sequence of pairs ending in the empty list results in a shorter sequence of pairs ending in the empty list, and hence is a list. Thus lists are closed under the operations of cons and cdr. Note that this is not quite right. What happens if you try to take the cdr of nil? In MIT Scheme you get an error, but by this definition it would be better to just return the empty list. Also notice that it would really be better to have distinctive operations for lists, as compared to pairs, e.g. adjoin, first and rest, instead of cons, car and cdr, to distinguish operations on lists from operations on pairs. For historical reasons, we stick with the latter, even though the better would be conceptually much cleaner. Slide 30.1.14 So let's use this to build a specific version of our abstraction. Note how our abstraction nicely inherits its contract from the underlying contract for pairs and lists. And note how points create a nested structure underneath lines. That is a line segment is a list of two elements, each of which points to another abstraction, namely a pair representing a vector. Slide 30.1.15 Okay, now let's put the pieces together. George is just defined as a collection (a list in this case) of line segments. All we need to do is take a rectangle or frame (which is just a pair of orthogonal line segments), and draw these segments within that rectangle. But as we start thinking towards the kinds of operations we did on George earlier, we would like to be able to draw George in different frames. So we would like to be able to define any frame, either of different size, or even non-orthogonal, and draw George inside of it. What does that mean? Ideally, we could take George (defined as a set of segments within a frame) and stretch those segments to fit within a new frame
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有