1.

What Is The Return Type Of Bounded-sqrt,bounded-sqrt-iter?

Answer»

The problem is in the conditional that has clauses that return values from different types: Number and BOOLEAN. In ORDER to accommodate such conditionals we allow union types in CONTRACT specifications. The resulting contracts:

Signature: bounded-sqrt(x,bound)

Purpose: To compute the square root of x, using Newton’s approximations method, if number of iterations does not exceed ’bound’

Type: [Number*Number -> Number union Boolean]

Example:

(sqrt 16. 7) should produce 4.000000636692939
(sqrt 16. 4) should produce #f

Pre-conditions: x >= 0, bound >= 0
Signature: bounded-sqrt-iter(guess,x,bound)

Purpose: To compute the square root of x, starting with ’guess’ as INITIAL guess, if number of iterations does not exceed ’bound’

Type: [Number*Number*Number -> Number union Boolean]

Example:

(sqrt 1 16. 7) should produce 4.000000636692939
(sqrt 1 16. 4) should produce #f
Pre-conditions: x >= 0, bound >= 0, guess != 0

The problem is in the conditional that has clauses that return values from different types: Number and Boolean. In order to accommodate such conditionals we allow union types in contract specifications. The resulting contracts:

Signature: bounded-sqrt(x,bound)

Purpose: To compute the square root of x, using Newton’s approximations method, if number of iterations does not exceed ’bound’

Type: [Number*Number -> Number union Boolean]

Example:

(sqrt 16. 7) should produce 4.000000636692939
(sqrt 16. 4) should produce #f

Pre-conditions: x >= 0, bound >= 0
Signature: bounded-sqrt-iter(guess,x,bound)

Purpose: To compute the square root of x, starting with ’guess’ as initial guess, if number of iterations does not exceed ’bound’

Type: [Number*Number*Number -> Number union Boolean]

Example:

(sqrt 1 16. 7) should produce 4.000000636692939
(sqrt 1 16. 4) should produce #f
Pre-conditions: x >= 0, bound >= 0, guess != 0



Discussion

No Comment Found