正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 6.1. 4 Types- simple data So now let's formalize this idea. We want to assign a type to irtually every kind of We have already seen some basic primitives: numbers, which -Number are a type; strings, another type; booleans, a third type; and symbols or names for things, to which we will be returning in a few lectures. For some things, like numbers, we could actually Boolean be more specific, for example distinguishing integers from real numbers As we saw with our motivating example, we typically want to know the type of a data structure in order to decide if some I procedure is appropriate to apply to that type. These basic primitive data types will usually be sufficient to handle the Slide 6.1.5 Types-compound data For compound data, we saw that the basic element was a pair, Pairs, B> and we have a type notation to denote one. Note that we A compound data structure formed by a cons pair, in include in this notation a specification of the types of the type B: e.g(cons 1 2)has type Pairs elements within the pair List<A>=PairsA List<A> or nil> We also saw that we could construct a list out of pairs. So we A compound data structure that is recursively defined can create a type notation for that as well. In essence we are s a pair, whose first element is of type A, and whose second element is either a list of type A or the empty creating a formal definition for a list: we specify that this compound data structure is recursively defined as a pair, whose E.g.(list 1 2 3)has type List snumber?: while( list 1 first element is of some type A, and whose second element is either another list or is the empty list. This exactly reflects the 60015c description we gave when we introduced lists Note that in our type specifications, we can include the possibility of alternative types of elements, as shown in the example Types-procedures Slide 6.1.6 Since procedures operate on objects, and return values, Now, we dont just have data structures in our language, we have procedures that operate on them. So we want to identify We will denote a procedures type by indicating the types of different types of procedures as well. We will do this by plus the symbol] to indicate that the arguments are creating a notation that indicates the number of arguments to a E.g.numbernumber specifies a procedure that takes a procedure, the type of each, and the type of the value returned mapped to the return value number as input, and returns a number as value by the procedure. For example, number maps to number would indicate a procedure that takes a single number as input and returns a number as output6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 6.1.4 So now let's formalize this idea. We want to assign a type to virtually every kind of expression in our language. We have already seen some basic primitives: numbers, which are a type; strings, another type; booleans, a third type; and symbols or names for things, to which we will be returning in a few lectures. For some things, like numbers, we could actually be more specific, for example distinguishing integers from real numbers. As we saw with our motivating example, we typically want to know the type of a data structure in order to decide if some procedure is appropriate to apply to that type. These basic primitive data types will usually be sufficient to handle the cases we will see. Slide 6.1.5 For compound data, we saw that the basic element was a pair, and we have a type notation to denote one. Note that we include in this notation a specification of the types of the elements within the pair. We also saw that we could construct a list out of pairs. So we can create a type notation for that as well. In essence we are creating a formal definition for a list: we specify that this compound data structure is recursively defined as a pair, whose first element is of some type A, and whose second element is either another list or is the empty list. This exactly reflects the description we gave when we introduced lists. Note that in our type specifications, we can include the possibility of alternative types of elements, as shown in the example. Slide 6.1.6 Now, we don’t just have data structures in our language, we have procedures that operate on them. So we want to identify different types of procedures as well. We will do this by creating a notation that indicates the number of arguments to a procedure, the type of each, and the type of the value returned by the procedure. For example, number maps to number would indicate a procedure that takes a single number as input and returns a number as output
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有