-1

I am trying to write a procedure that returns a closure. I want to be able to apply any procedure to my series. This is what I have so far and it is not working for me.

(define (forAll n m)
   (lambda (op start)
      (op (op n start) (+ n 1) m)))

I want to be able to do these operations:

(define my_iterator (forAll 1 5))       
(my_iterator + 0) → 15
(my_iterator * 1) → 120
(my_iterator (lambda (x y) (display x)(display " ")) "") → 1 2 3 4 5

1 Answers1

0

You need to give the local procedure a name so that you can recurse on it and it needs to have more arguments than your contract since you it needs to pass state along for each iteration. Here is a skeleton of what you roughly need to implement:

(define (for-all from to)
  (define (helper op start from)
    (if stop-condition-expression
        start
        (helper op new-start-expression new-from-expression)))

  (lambda (op start)
    (helper op start from)))
Sylwester
  • 47,942
  • 4
  • 47
  • 79