简体   繁体   中英

Can logical operators use tail-recursion

(define (fast-prime? n times)
  (cond ((= times 0) true)
        ((fermat-test n) (fast-prime? n (- times 1)))
        (else false)))

This code is from SICP , chapter 1.2.6. The procedure uses tail-recursion , so it doesn't cause linear recursion when computed, in other words it's iterative . If I rewrite the procedure using or / and instead of cond , will it cause linear recursion ?

(define (fast-prime? n times)
  (or (= times 0)
      (and (fermat-test n) (fast-prime? n (- times 1)))))

andor的右侧是尾部表达式(请参见R5RS第3.5节 ),因此可以安全地将其用于迭代目的。

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