1
将参数顺序交换到-
函数时发生问题。为什么`-`参数顺序会导致Racket REPL内存不足?
; source
(define (compose f g) (lambda (x) (f (g x))))
(define (repeated f n)
(if (= n 1)
f
(compose f (repeated f (- 1 n))) ; causes an out of memory error
(compose f (repeated f (- n 1))) ; runs without issue
))
(define (square n) (* n n))
((repeated square 2) 6) ; 1296
; REPL
> > Racket virtual machine has run out of memory; aborting
Aborted (core dumped)
问题在于如果硬编码值。此外,如果我使用+
增加n
,则问题不适用。