I wrote a simple derivative function in scheme today. I was asked to return a function such that g(x) = (f (x+h) -f(x))/h . Does this suffice to return a function or does this only return a value?
(define (der f h)
(lambda (x)
(/ (- (f(+ x h)) (f x)) h)))
Yes, the code in the question is returning a function (that's what the
lambda
is for). If it were returning a value, it'd be missing the line with(lambda (x)
and the corresponding closing parentheses.Also notice that although the procedure is correct, the formula stated in the question isn't right, it should be:
As a side note, the correct way to use the derivative function as defined would be: