传统的递归阶乘和尾递归阶乘

时间:2014-04-08 11:09:28

标签: racket

(define factorial 
  ( lambda (n )
     (if( = n 0 ) 
        1
        ( * n (factorial (- n 1 ) )))))
(factorial 5

(define factorial-tail
  ( lambda (n acc)
     (if( = n 0 ) 
        acc
      (factorial-tail (- n 1)  (* acc n )))))

(factorial-tail 5 1)

您好, 让我们说你有两个功能,因为我出现了。我要求你在两种情况下向我展示性能堆栈(堆栈帧)。 问候;)

1 个答案:

答案 0 :(得分:3)

您可以使用trace

#lang racket

(require racket/trace)

(define factorial 
  (lambda (n)
    (if (= n 0) 
        1
        (* n (factorial (- n 1))))))
(trace factorial)
(factorial 5)
;; Output:
;; >(factorial 5)
;; > (factorial 4)
;; > >(factorial 3)
;; > > (factorial 2)
;; > > >(factorial 1)
;; > > > (factorial 0)
;; < < < 1
;; < < <1
;; < < 2
;; < <6
;; < 24
;; <120
;; 120

(define factorial-tail
  (lambda (n acc)
    (if (= n 0) 
        acc
        (factorial-tail (- n 1)  (* acc n )))))
(trace factorial-tail)
(factorial-tail 5 1)
;; Output:
;; >(factorial-tail 5 1)
;; >(factorial-tail 4 5)
;; >(factorial-tail 3 20)
;; >(factorial-tail 2 60)
;; >(factorial-tail 1 120)
;; >(factorial-tail 0 120)
;; <120
;; 120