正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 13.1.12 An Alternative view of data: So what, you say! Well, we can use this idea to captu Procedures with State information about a data structure. In particular, we can use a procedure to represent data objects with state. What would that A procedure has parameters and body as specified by a expression mean? It would say that we could use the local environment of environment (which can hold name-value bindings the procedure plus its parameters to hold the values of the late(and hide) data, and datum, and we could create local procedures within the data procedure to manipulate these values, to change the state of the Need access to that environment ructor, accessors, mutators, predicates This means that the only access to the values of the data object ion: changes in the private state of the will be through the procedure representing the data. This would 6 001 SICP I nicely encapsulate the data structure inside this procedure This probably still sounds odd so let's look at a specific 6.001 Notes: Section 13.2 Slide 13.2.1 Example: Pair as a Procedure with State To illustrate this idea of using a procedure to represent a data structure,an object with state, let's look at the following, rather(define (cons x y) dd, example Here is a very different way of implementing a cond《(eq?mg'cAR)x) ((eq? m84 'CDR) y) cons cell or a pair. Let me stress that this is not the way that 《eq?m8 f PAIR?)# else (error pair cannot msg))))) Scheme normally represents pairs. Of course, the idea of data abstraction is that the actual implementation of a data structure should be irrelevant to the user. This example is used to drive home a conceptual point Here, we have implemented a pair as a procedure! Thus our undamental data structure is now a procedure rather than some 60015c storage in memory slots Example: Pair as a Procedure with State Slide 13.2.2 Look at this carefully. First, note that cons, as defined here involves two lambdas Remem ber that there is a hidden ((eq? m84CDR lambda inside the syntactic sugar of this definition. This ((eqr? msg 'PAIR?) #t) means that there is a second l ambda as the body of the cons and thus when we evaluate(cons x y)using this pa ntation, we get value. a procedure of one parameter, ms g So what does this say? It says that when we use cons with this implementation our representation for our fundamental way of gluing things together is now a procedure of one argument So what would that cons thing do? Since it is a procedure, if we send it a value, or if we apply the procedure to a single argument, note what it does. It uses the value of the argument, in this case a particular symbol, to decide6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 13.1.12 So what, you say! Well, we can use this idea to capture information about a data structure. In particular, we can use a procedure to represent data objects with state. What would that mean? It would say that we could use the local environment of the procedure plus its parameters to hold the values of the datum, and we could create local procedures within the data procedure to manipulate these values, to change the state of the object. This means that the only access to the values of the data object will be through the procedure representing the data. This would nicely encapsulate the data structure inside this procedure. This probably still sounds odd so let's look at a specific example. 6.001 Notes: Section 13.2 Slide 13.2.1 To illustrate this idea of using a procedure to represent a data structure, an object with state, let's look at the following, rather odd, example. Here is a very different way of implementing a cons cell or a pair. Let me stress that this is not the way that Scheme normally represents pairs. Of course, the idea of data abstraction is that the actual implementation of a data structure should be irrelevant to the user. This example is used to drive home a conceptual point. Here, we have implemented a pair as a procedure! Thus our fundamental data structure is now a procedure rather than some storage in memory slots. Slide 13.2.2 Look at this carefully. First, note that cons, as defined here, involves two lambdas. Remember that there is a hidden lambda inside the syntactic sugar of this definition. This means that there is a second lambda as the body of the cons and thus when we evaluate (cons x y) using this particular implementation, we get back as a value, a procedure of one parameter, msg. So what does this say? It says that when we use cons with this implementation our representation for our fundamental way of gluing things together is now a procedure of one argument. So what would that cons thing do? Since it is a procedure, if we send it a value, or if we apply the procedure to a single argument, note what it does. It uses the value of the argument, in this case a particular symbol, to decide
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有