简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM