正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 11.1.3 Let's start by looking at very simple data structures. For Primitive Data example, we can actually consider a variable or name to be a creates a new binding for name kind of data abstraction special form If we take this view then our "constructor" in this case is the special form define. It binds the name (or symbol) given returns value bound to name by the first argument to the value of the second expression. The key point is that we can consider this as a kind of primitive data abstraction that pairs up a name and a value The associated selector in this case is very simple. We just use the name itself, that is, when we evaluate the name, it looks up the value bound to it by the define expression, and returns 4 6001 sICP that value, that is, it pulls apart the binding of name and value, and returns one component of that binding Primitive Data Slide 1l. 1.4 (define x 10) creates a new binding for name Now we can introduce the ability to mutate that variable, that is special form the expression that accomplishes this, which is the set at to change the binding of the name and a value. Let's look firs eturns value bound to name expression shown To Mutate changes the binding for name This is also a special form, as it doesn't follow the normal rules for combinations The rule for evaluation of this form is take the second argument and evaluate it using standard rules then take the first argument and just treat it as a symbol (i.e. don't evaluate it). Now, find the binding of that symbol and change it 1001 SICP to take on the new value Note that this looks a lot like a define expression, but as we will see in a few lectures there is a fundamental difference. For now the distinction is that a de fine would create a new binding for the name, using up additional space, whereas set! changes an existing binding Slide 11.1.5 Assignment-- set! What does adding mutation do to our language? One of the primary effects of adding mutation is that we end up breaking 【 define x10 our substitution model. This is okay, as we will shortly replace +x5)=>15 each time it ey it with a better model +x5)=>15 same scope In fact, the substitution model inherently assumed that we were dealing with functional programming, with no mutation or side effects what does this mean? In essence. functional programming implies that we can conceptually treat our procedures as if they were mathematical fu know that we have procedures that deal with things other thar numbers but the same idea holds This means that we could treat our procedures as mappings from input values to output values, and more importantly, that mapping was consistent, providing the same output value for a particular input value no matter when we did the actual evaluation For example, if i define x to have the value 10, and then I evaluate the expression ( x 5), i of course get the value 15. If I then do some other computation and return to the evaluation of ( x 5)I still get the value 156.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 11.1.3 Let's start by looking at very simple data structures. For example, we can actually consider a variable or name to be a kind of data abstraction. If we take this view, then our "constructor" in this case is the special form define. It binds the name (or symbol) given by the first argument to the value of the second expression. The key point is that we can consider this as a kind of primitive data abstraction that pairs up a name and a value. The associated selector in this case is very simple. We just use the name itself, that is, when we evaluate the name, it looks up the value bound to it by the define expression, and returns that value, that is, it pulls apart the binding of name and value, and returns one component of that binding. Slide 11.1.4 Now we can introduce the ability to mutate that variable, that is, to change the binding of the name and a value. Let's look first at the expression that accomplishes this, which is the set! expression shown. This is also a special form, as it doesn't follow the normal rules for combinations. The rule for evaluation of this form is: take the second argument and evaluate it using standard rules; then take the first argument and just treat it as a symbol (i.e. don't evaluate it). Now, find the binding of that symbol and change it to take on the new value. Note that this looks a lot like a define expression, but as we will see in a few lectures, there is a fundamental difference. For now, the distinction is that a define would create a new binding for the name, using up additional space, whereas set! changes an existing binding. Slide 11.1.5 What does adding mutation do to our language? One of the primary effects of adding mutation is that we end up breaking our substitution model. This is okay, as we will shortly replace it with a better model. In fact, the substitution model inherently assumed that we were dealing with functional programming, with no mutation or side effects. What does this mean? In essence, functional programming implies that we can conceptually treat our procedures as if they were mathematical functions. Yes, we know that we have procedures that deal with things other than numbers, but the same idea holds. This means that we could treat our procedures as mappings from input values to output values, and more importantly, that mapping was consistent, providing the same output value for a particular input value, no matter when we did the actual evaluation. For example, if I define x to have the value 10, and then I evaluate the expression (+ x 5), I of course get the value 15. If I then do some other computation and return to the evaluation of (+ x 5) I still get the value 15
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有