正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology tests for types, error checks to catch unexpected types of arguments, and disciplined use of tags to tell use how to handle expressions rather than assuming an expression is of a particular type Thus, as we go through this exercise, watch how both of these themes guide the development of the system Slide 9. 2.5 Approach: start simple, then extend Here is our plan to accomplish this. We will first build an evaluator to simplify expressions involving normal numbers nges and limited-precision values Our second version will extend this base version in an obvious manner. We will see that this obvious extension is actually Goal: bu a way that it will extend easily safely flawed, and we will use the insights from its failure to create a data-directed programming Safel es defensive programming correct extension using a series of versions of the evaluator This will be a cycle in which we extend the system, observe it .Today: multiple versions of eval behavior, and use the observation to guide the next extension Thus this series of extensions will highlight how data-directed " Extend the evaluator, observe bugs eval-3 through-7 Do it again with tagged data programming and defensive programming intertwine to guide 6001 ICP the development of a complex system 1. ADT (Abstract Data Type)for sums Slide 9.2.6 type: ExP, Exp Let's start with our simple"sum"expressions. First we build a (define (make-sum addend augen constructor, as shown. Notice the type of this constructor. It (list 1+ addend augend)) takes in any two expressions, and returns a SumExp something of a more particular type, which is identified by the symbol t at the front of the expression. This symbol serves as the tag for the expression, and we are taking advantage of the fact that the operator itself identifies the type of expression Thus this constructor creates a tagged data type of particular type"sum", by gluing together the subordinate expressions with 0O1 sIcP the te Slide 9.2.7 1. ADT (Abstract Data Type)for sums Associated with this object type will be a predicate to detect type: ExP, Exp - SumExp objects. Sum-exp? takes in an expression(define(make-sum addend augend of any type, and returns a boolean value, that value indicating (list addend augend)) whether the input expression is a sum. Check out the body of type: anytype - boolean this procedure. We first check to see that the expression is a pair (and (pair? e)(eq? (car e). +)) (not that and evaluates its arguments in a left to right order, and stops as soon as one of its arguments returns a false value). If it is not, we stop, and return false. If it is a pair, then we can safely take the car of it, and check for the symbol 6001 SICP + Note that this is an instance of defensive programming Also note how we are using the tag to identify the type of object6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. tests for types, error checks to catch unexpected types of arguments, and disciplined use of tags to tell use how to handle expressions rather than assuming an expression is of a particular type. Thus, as we go through this exercise, watch how both of these themes guide the development of the system. Slide 9.2.5 Here is our plan to accomplish this. We will first build an evaluator to simplify expressions involving normal numbers. Our second version will extend this base version in an obvious manner. We will see that this obvious extension is actually flawed, and we will use the insights from its failure to create a correct extension, using a series of versions of the evaluator. This will be a cycle in which we extend the system, observe its behavior, and use the observation to guide the next extension. Thus this series of extensions will highlight how data-directed programming and defensive programming intertwine to guide the development of a complex system. Slide 9.2.6 Let's start with our simple "sum" expressions. First we build a constructor, as shown. Notice the type of this constructor. It takes in any two expressions, and returns a SumExp, something of a more particular type, which is identified by the symbol + at the front of the expression. This symbol serves as the tag for the expression, and we are taking advantage of the fact that the operator itself identifies the type of expression. Thus this constructor creates a tagged data type of particular type "sum", by gluing together the subordinate expressions with the tag. Slide 9.2.7 Associated with this object type will be a predicate to detect instances of such objects. Sum-exp? takes in an expression of any type, and returns a Boolean value, that value indicating whether the input expression is a sum. Check out the body of this procedure. We first check to see that the expression is a pair (not that and evaluates its arguments in a left to right order, and stops as soon as one of its arguments returns a false value). If it is not, we stop, and return false. If it is a pair, then we can safely take the car of it, and check for the symbol +. Note that this is an instance of defensive programming. Also note how we are using the tag to identify the type of object
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有