Writing a forAll procedure that takes two argument

2019-03-06 19:42发布

问题:

I am trying to write a forAll procedure that takes two arguments: the start and end values of a series. The resulting closure expects two arguments as well: an operation to apply to all elements in the series, as well as an initial value.

This is what I have and, I seem to be missing something or I don't understand the concept behind closures.

(define (forAll n m)
   (if (>= n m) '()
   (forAll (+ n 1) m))

 (lambda (op start) (op start n m))
       )

回答1:

This looks like a "combine existing functions into new ones" exercise.
Start with writing a function that generates the list of numbers if you haven't already done that.
Use that to make this function.
Review what you've learned recently and see if you've already got a function that does something similar to what the closure should do.

What you end up with will probably look like this:

(define (forAll n m)
    (lambda (op start)
        (... code that builds a list and computes the result ...)))