正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology And ideally we would like the process of gluing things together Compound data to have the property of closure, that is, that whatever we get by Need a way of gluing data ele together into a unit gluing things into a compound structure can be treated as a that can be treated as a simple Need ways of getting the piece primitive so that it can be the input to another gluing operation Not all ways of creating compound data have this property, but Need a contract between the "glue"and the "unglue the best of them do, and we say they are closed under the operation of creating a compound object if the result can itself of clos ined by creating a compound data be a primitive for the same compound data construction process he e treat f othem itive object and Slide 5.2.6 (cons <x-exp> <y-exp>) Scheme's basic means for gluing things together is called cons, short for constructor, and virtually all other methods andrv -exp evaluates te a valve uv> for creating compound data objects are based on cons Returns a pair <p>whose Cons is a procedure that takes two expressions as input. It (eax<P>)=><x-a1> evaluates each in turn, and then glues these values together into eturns the car-part of the pair <P> something called a pair. Note that the actual pair object is the value returned by evaluating the cons. The two parts of a cons (cdr<>)·><-va Returns the cdr-part af the pair <P pair are called the car and the edr, and if we apply the DOI SICP argument that was evaluated when the pair was cia e ocedures of those names to a pair, we get back the value of the Note that there is a contract here between cons car and cdr. in which cons glues things together in some arbitrary manner, and all that matters is that when car for example, is applied to that object, it gets back out what we started with Slide 5.2.7 Com pound Data Note that we can treat a pair as a unit, that is, having built a pair, .T ye can treat it as a primitive and use it anywhere we might use any other primitive. So we can pass a pair in as input to some other data abstraction, such as another pair 6001 SICP6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 5.2.5 And ideally we would like the process of gluing things together to have the property of closure, that is, that whatever we get by gluing things into a compound structure can be treated as a primitive so that it can be the input to another gluing operation. Not all ways of creating compound data have this property, but the best of them do, and we say they are closed under the operation of creating a compound object if the result can itself be a primitive for the same compound data construction process. Slide 5.2.6 Scheme's basic means for gluing things together is called cons, short for constructor, and virtually all other methods for creating compound data objects are based on cons. Cons is a procedure that takes two expressions as input. It evaluates each in turn, and then glues these values together into something called a pair. Note that the actual pair object is the value returned by evaluating the cons. The two parts of a cons pair are called the car and the cdr, and if we apply the procedures of those names to a pair, we get back the value of the argument that was evaluated when the pair was created. Note that there is a contract here between cons, car and cdr, in which cons glues things together in some arbitrary manner, and all that matters is that when car, for example, is applied to that object, it gets back out what we started with. Slide 5.2.7 Note that we can treat a pair as a unit, that is, having built a pair, we can treat it as a primitive and use it anywhere we might use any other primitive. So we can pass a pair in as input to some other data abstraction, such as another pair
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有