正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 7.1.13 Second, describe the types of values used in the computation An example of code documentation In this case, the inputs or parameters are both numbers, and the returned value is also a number. Actually, if we were more 7 compute approxinate square root by nement,ques⊥ g curre careful here, we would require that X be a positive number, and ;:appx。x⊥mat。n,X⊥8 number who器e8 quare we would place a check somewhere to ensure that this is true ot we are seek⊥ng Type: (number, number)> number E(good-enuf?宮gues 6001 sICP An example of code documentation Slide 7.1.14 Third, describe constraints, either desired or required, on the 1 ambda《xgu it: compute approxinate square root by computation. Here, we know that squaring the guess should get ve refinement, guess is current ;:;apx。 ximation, x is nunber wh。 Go square us something close to the target value, although we really don't i root we are seekins guarantee this until we reach the termination stage ood-enaf?xgue器 qrrt-helper x 48m0 6001 SICP Slide 7.1.15 An example of code documentation And fourth, describe the expected state of the computation ar the goal at each stage in the process. For example, here we indicate what good-enuf? should do, namely test if 77:ucc容 ve refinement,日ue器 current quaro our approximation is sufficiently accurate. Then we indicate 7: Type: (number, number)- number that if this is the case, we can stop and what value to return to (ood-enuf?xque器器); can we stop? satisfy the contract of the entire procedure. And we indicate f yes, return how to continue the process, though we could probably say a it more about what improve should do then get better cue图s Notice how we can use the documentation to check some aspects of our procedure's"contract". Here, we have indicated 8n003 60015e that the procedure should return a number. By examining the f expression, we can see that in the consequent clause, if the input parameter guess is a number, then we are guaranteed to return a number. For the alternative clause, we can use induction to reason that given numbers as input, we also return a number, and hence the entire procedure returns a value of the correct type6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 7.1.13 Second, describe the types of values used in the computation. In this case, the inputs or parameters are both numbers, and the returned value is also a number. Actually, if we were more careful here, we would require that X be a positive number, and we would place a check somewhere to ensure that this is true. Slide 7.1.14 Third, describe constraints, either desired or required, on the computation. Here, we know that squaring the guess should get us something close to the target value, although we really don’t guarantee this until we reach the termination stage. Slide 7.1.15 And fourth, describe the expected state of the computation and the goal at each stage in the process. For example, here we indicate what good-enuf? should do, namely test if our approximation is sufficiently accurate. Then we indicate that if this is the case, we can stop and what value to return to satisfy the contract of the entire procedure. And we indicate how to continue the process, though we could probably say a bit more about what improve should do. Notice how we can use the documentation to check some aspects of our procedure’s “contract”. Here, we have indicated that the procedure should return a number. By examining the if expression, we can see that in the consequent clause, if the input parameter guess is a number, then we are guaranteed to return a number. For the alternative clause, we can use induction to reason that given numbers as input, we also return a number, and hence the entire procedure returns a value of the correct type
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有