正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 9.2.2 Example: Arith metic evaluation Building a system to evaluate expressions such as those just shown would be interesting in its own right, but I would like to (define expl m"m+t3120 add more to my system. In particular, I want my system to not (eval-1 expl) ==>38 only simplify standard arithmetic expressions, but also expressions that involve numbers that are only known to lie Expressions might include values other than numbers within a particular range. For example, I want my system to be e unknown number between min and may able to simplify expressions where the numbers are only known arithmetic:3,7]+[13]=[4,10] to lie between specific bounds of uncertainty. In the example Limited pr shown, I may only know that one number lies between 3 and 7, some value t some error amount rithmetic:(100±1)+(3±0.5)=(103±1.5) and another number lies between I and 3, but I still want my OOt sIcP system to be able to simplify an addition involving these numbers whose result i know must lie between 4 and 10 Moreover, I would like my system to be able to deal with expressions involving data from scientific experiments, in which I know the ostensible value of a number and a specified range of precision, that is, that the true value may lie within some plus/minus range of the ostensible value. In the example shown, I want my system to be able to add together expressions where I know one number is 100 plus/minus 1 and the other number is 3 plus/minus 0.5, and ave the system deduce that the sum should be 103 plus/minus 1.5 Thus, my goal is to build an arithmetic evaluation system that reduces arithmetic expressions, consisting of a mix of symbols and numbers, to simplest form; where the expressions may be standard numbers, ranges of values or imited precision values Slide 9.2.3 Approach: start simple, then extend The basic approach we are going to take is to start with easy Characteristic of all software engineering projects things first, an obvious thing to do! In our case, we will first tart with eval for numbers, then add support for build a system to handle normal numbers, and then we will look at how to build on top of that to handle other expressions, like ranges and limited precision values This does sound obvious, but it is important to stress that this is an important characteristic of a well-designed software engineering project, (and one that all too often is not followed, even in commercial systems). It is almost al ways easier to extend a base system, than to try to do the whole thing at once. 4 This is an important lesson to learn, and one that hopefully you will see in this extended example Approach: start simple, then extend Slide 9.2.4 Characteristic of all software engineering projects One of the things to watch for as we go through this exercise is Start with eval for numbers, then add support fo ranges and limited-precision values to notice how, by doing the development in stages, we make extensions to the system much easier to conceptualize Indee -Goal: build eval in a way that it will extend easily safely our goal is to build our simple evaluator so that extensions are Easily: requires data-directed pro safely requires defensive programming both easy and safe So what does that mean To allow for easy extensions, we will need to utilize tools from data-directed programming. That is, if we use data-directed programming tools(e.g. tagged data, dispatch to methods based on tag types), we will see it is easy to 6001 SICP I add new types of expressions to our system(create a new structure, an appropriate tag, and a dispatch to a method to handle that new type) To allow for safe extensions, we will need to use methods from defensive programming. This will require explicit6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 9.2.2 Building a system to evaluate expressions such as those just shown would be interesting in its own right, but I would like to add more to my system. In particular, I want my system to not only simplify standard arithmetic expressions, but also expressions that involve numbers that are only known to lie within a particular range. For example, I want my system to be able to simplify expressions where the numbers are only known to lie between specific bounds of uncertainty. In the example shown, I may only know that one number lies between 3 and 7, and another number lies between 1 and 3, but I still want my system to be able to simplify an addition involving these numbers, whose result I know must lie between 4 and 10. Moreover, I would like my system to be able to deal with expressions involving data from scientific experiments, in which I know the ostensible value of a number and a specified range of precision, that is, that the true value may lie within some plus/minus range of the ostensible value. In the example shown, I want my system to be able to add together expressions where I know one number is 100 plus/minus 1 and the other number is 3 plus/minus 0.5, and have the system deduce that the sum should be 103 plus/minus 1.5. Thus, my goal is to build an arithmetic evaluation system that reduces arithmetic expressions, consisting of a mix of symbols and numbers, to simplest form; where the expressions may be standard numbers, ranges of values or limited precision values. Slide 9.2.3 The basic approach we are going to take is to start with easy things first, an obvious thing to do! In our case, we will first build a system to handle normal numbers, and then we will look at how to build on top of that to handle other expressions, like ranges and limited precision values. This does sound obvious, but it is important to stress that this is an important characteristic of a well-designed software engineering project, (and one that all too often is not followed, even in commercial systems). It is almost always easier to extend a base system, than to try to do the whole thing at once. This is an important lesson to learn, and one that hopefully you will see in this extended example. Slide 9.2.4 One of the things to watch for as we go through this exercise is to notice how, by doing the development in stages, we make extensions to the system much easier to conceptualize. Indeed, our goal is to build our simple evaluator so that extensions are both easy and safe. So what does that mean? To allow for easy extensions, we will need to utilize tools from data-directed programming. That is, if we use data-directed programming tools (e.g. tagged data, dispatch to methods based on tag types), we will see it is easy to add new types of expressions to our system (create a new structure, an appropriate tag, and a dispatch to a method to handle that new type). To allow for safe extensions, we will need to use methods from defensive programming. This will require explicit
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有