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
,則問題不適用。