正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 8.1.8 If you think carefully about it, you should see that in the Say your favorite color case, I got the meaning associated with the ey Say "your favorite color favorite color", much like getting the value as pressiOn foA>t ociated with a What is the difference? name. In the second case, I got the actual expression. The In one case, we want the meaning associated with the double quotation marks"in the second case indicated that I In the other case, we want the actual words (or symbols) wanted the actual expression, while in the first case I want the f the expres value associated with it(i.e. the actual favorite color vs the hrase favorite color ) So in many cases we may want to b able to make exactly this distinction, between the value ed with an ex and the actual symb L expression itself. This is going to lead us to introduce a new data type Slide 8.1.9 Creating and Referencing Symbols Now, the question is how do I create symbols as data objects? Well, we already saw one way of doing this, when we defined a (define alpha 27) name for a value And we saw that if we wanted to get back the value associated. How do I reference a symbol's value? ith that symbol (or name)we could just reference it, and the yue: 27 valuator would return the associated value But suppose I want to reference the symbol itself. How do i do How do I reference the symbol itseif? that? In other words, how do i distinguish between"your ??? favorite color"and"blue"as the value of your favorite color" 600SC Slide 8.1.10 y of telling interpreter: "I want the following Basically, we need to back up and think about what the scheme data structure, not as an expression to be interpreter is doing. When we type in an expression and ask for it to be evaluated, the reader first converts that expression into an internal form, and the evaluator then applies its set of rules to determine the value of the expression Here, we need a way of telling the reader and evaluator that we don 't want to get the value. Scheme provides this for us with a special form, called quote. If we evaluate the example expression using this special form, it returns for us a value of I the type symbol that somehow captures that name Note that it makes sense for quote to be a special form. We can, t use normal evaluation rules because that would cause us to get the value associated with the name alpha but in fact our goal is to simply keep the name, not its value6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 8.1.8 If you think carefully about it, you should see that in the first case, I got the meaning associated with the expression "your favorite color", much like getting the value associated with a name. In the second case, I got the actual expression. The "double quotation marks" in the second case indicated that I wanted the actual expression, while in the first case I want the value associated with it (i.e. the actual favorite color vs. the phrase "favorite color"). So in many cases we may want to be able to make exactly this distinction, between the value associated with an expression, and the actual symbol or expression itself. This is going to lead us to introduce a new data type. Slide 8.1.9 Now, the question is how do I create symbols as data objects? Well, we already saw one way of doing this, when we defined a name for a value. And we saw that if we wanted to get back the value associated with that symbol (or name) we could just reference it, and the evaluator would return the associated value. But suppose I want to reference the symbol itself. How do I do that? In other words, how do I distinguish between "your favorite color" and "blue" as the value of "your favorite color". Slide 8.1.10 Basically, we need to back up and think about what the Scheme interpreter is doing. When we type in an expression and ask for it to be evaluated, the reader first converts that expression into an internal form, and the evaluator then applies its set of rules to determine the value of the expression. Here, we need a way of telling the reader and evaluator that we don't want to get the value. Scheme provides this for us with a special form, called quote. If we evaluate the example expression using this special form, it returns for us a value of the type symbol that somehow captures that name. Note that it makes sense for quote to be a special form. We can't use normal evaluation rules because that would cause us to get the value associated with the name alpha but in fact our goal is to simply keep the name, not its value
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有