1

How do I make a procedure from a function that makes procedures recursive?

for example, lets have a function that returns a procedure, the returned procedure will take two arguments (x and y). When called with z as an argument it will recursively call itself until z fulfills some requirements

(define test
  (lambda (x y)
    (lambda z
      (if (> z 100)
          z
          (RecursiveCallToChangeValueOfZ (+ x y z))))))

1 Answer 1

2

Here are three variations:

#lang racket

;; use internal definition
(define test
  (lambda (x y)
    (define f
      (lambda z
        (if (> z 100)
            z
            (f (+ x y z)))))
    f))

;; use letrec (which internal definition expands to    
(define test2
  (lambda (x y)
    (letrec ([f (lambda z
                  (if (> z 100)
                      z
                      (f (+ x y z))))])
      f)))

(require mzlib/etc)
;; use rec (a little syntactic sugar that expands to the previous solution)
(define test3
  (lambda (x y)
    (rec f (lambda z
             (if (> z 100)
                 z
                 (f (+ x y z)))))))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.