SLq
SLq

Reputation: 125

scheme recursive function with multiple lambdas

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))))))

Upvotes: 1

Views: 740

Answers (1)

soegaard
soegaard

Reputation: 31145

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)))))))

Upvotes: 2

Related Questions