Exercise 196: Develop a-function=. The function determines whether two functions from numbers to numbers produce the same results for 1.2, 3, and -5.775.
Can we hope to define function=?, which determines whether two functions from numbers to numbers are equal?
Answer
; returns true if the functions supplied return the same value for 3
; different numbers
; Function, Function -> Booleas
(define (a-function= x y)
(and (a-helper x y 1.2) (a-helper x y 3) (a-helper x y -5.775)))
; tests if the two functions supplied return the same number
; Function, Function, Number -> Boolean
(define (a-helper x y number)
(= (x number) (y number)))
; some test functions - both 1 and 3 should return the same value, 2 should not.
(define (test1 x) (* 2 x))
(define (test2 x) (* 3 x))
(define (test3 x) (+ x x))
; and some tests
(check-expect (a-function= test1 test2) false)
(check-expect (a-function= test1 test3) true)
Can we hope to define function=?, which determines whether two functions from numbers to numbers are equal?
We cannot (depending on how you define 'equal functions'). We can check to see the return value of the functions are equal for a specified set of arguments, but without the code introspection offered by languages such as Ruby, you can't see what is inside these black box functions.
No comments:
Post a Comment