正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology the procedures Scheme is a weakly typed language, meaning that the programmer does not specify the types of inputs and outputs. Thus, errors will not be caught until run-time, which can make debugging harder, but the programmer tends to have more flexibility in creating procedures. Even in a weakly typed language, a good programmer will take advantage of the discipline of type analysis Slide 6l.ll Types, precisely More formally, we see that a type actually describes many A type describes a set of scheme values procedures, basically any procedure that maps the specified number> number describes the set number and type of inputs to the specified type of output all procedures, whose result is a numbe We also see that not only does every Scheme value have a type, which require one argument that must be a number but that in some cases, a value may be described by several types(for example an integer is also a kind of number) .When Every scheme value has a type Some values can be described by multiple types this happens, we will typically choose the type which describes If so, choose the type which describes the largest set the largest set of objects as our designated type. Thus, for example, addition maps two integers to an integer, but it also Special form keywords like define do not name values therefore special form keywords have no type maps two numbers (including real numbers )to a number, so we 6001SC would use this latter type as our specification for addition Finally, note that some special forms do not name values, and hence do not have a type associated with them Your turn Slide 6.1.12 The following expressions evaluate to values of what type? Let's see if you are getting this. Here are three example expressions. If evaluate each of them, you will get some (lambda (a b e)(if(>a o)(+ b e) (- b a))) Scheme object, and you should be able to reason through to determine the type of that returned value. When you are ready to see the answers, go to the next slide (lambda (p) (if p hi "."bye") (3.14(*25) Slide 6.1.13 Your turn We know that the type of the first expression should be a procedure, since we know that lambda will create a procedure The following expressions evaluate to values of what type? We also know that the number of arguments for this procedure (lambda (a b c) (if(>a o)(+ b c)(-b a)) 3. In terms of the types of the arguments, we can see that the number, number, numbe number first one must be a number, since greater than expects a number as argument. The other two arguments must also be numbers, (lambda (p) (if p hi""bye") since they are used with procedures that take numbers as input Finally, the value returned by this procedure is a number, since (*3.14(25)) both the consequent and the alternative clauses of the if expression return numbers. Hence, we have the type declaration 4 shown here, a procedure that maps three numbers to a number6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. the procedures. Scheme is a weakly typed language, meaning that the programmer does not specify the types of inputs and outputs. Thus, errors will not be caught until run-time, which can make debugging harder, but the programmer tends to have more flexibility in creating procedures. Even in a weakly typed language, a good programmer will take advantage of the discipline of type analysis. Slide 6.1.11 More formally, we see that a type actually describes many procedures, basically any procedure that maps the specified number and type of inputs to the specified type of output. We also see that not only does every Scheme value have a type, but that in some cases, a value may be described by several types (for example an integer is also a kind of number). When this happens, we will typically choose the type which describes the largest set of objects as our designated type. Thus, for example, addition maps two integers to an integer, but it also maps two numbers (including real numbers) to a number, so we would use this latter type as our specification for addition. Finally, note that some special forms do not name values, and hence do not have a type associated with them. Slide 6.1.12 Let's see if you are getting this. Here are three example expressions. If evaluate each of them, you will get some Scheme object, and you should be able to reason through to determine the type of that returned value. When you are ready to see the answers, go to the next slide. Slide 6.1.13 We know that the type of the first expression should be a procedure, since we know that lambda will create a procedure. We also know that the number of arguments for this procedure is 3. In terms of the types of the arguments, we can see that the first one must be a number, since greater than expects a number as argument. The other two arguments must also be numbers, since they are used with procedures that take numbers as input. Finally, the value returned by this procedure is a number, since both the consequent and the alternative clauses of the if expression return numbers. Hence, we have the type declaration shown here, a procedure that maps three numbers to a number
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有