Recursion 河内塔楼方案(递归)

Recursion 河内塔楼方案(递归),recursion,scheme,towers-of-hanoi,Recursion,Scheme,Towers Of Hanoi,我今天在scheme中编写了以下代码,但是评估是错误的。请不要告诉我我在编程方面很差劲,我知道这是一个典型的递归问题,但我遇到了麻烦: (define (towers-of-hanoi n source temp dest) (if (= n 1) (begin (display "Move the disk from ") (display source) (display " to " ) (display dest)

我今天在scheme中编写了以下代码,但是评估是错误的。请不要告诉我我在编程方面很差劲,我知道这是一个典型的递归问题,但我遇到了麻烦:

(define (towers-of-hanoi n source temp dest)
 (if (= n 1)
  (begin (display "Move the disk from ")
         (display source) 
         (display " to " )
         (display dest)
         (newline))
 (begin (towers-of-hanoi (- n 1) source temp dest)
        (display "Move the disk from ") 
        (display source)
        (display " to ")
        (display dest)
        (newline)
  (towers-of-hanoi(- n 1) temp source dest))))

我希望代码能够工作,而当我调试它时,我只是更加困惑自己。有人能帮我吗?

在您的代码中,最后一个递归调用似乎是错误的,并且过程参数的顺序有问题。请尝试以下方法:

(define (towers-of-hanoi n source dest temp)
  (if (= n 1)
      (begin 
        (display "Move the disk from ")
        (display source) 
        (display " to " )
        (display dest)
        (newline))
      (begin 
        (towers-of-hanoi (- n 1) source temp dest)
        (display "Move the disk from ") 
        (display source)
        (display " to ")
        (display dest)
        (newline)
        (towers-of-hanoi (- n 1) temp dest source))))
我注意到你一直在问被标记为
racket
的问题,这里有一个关于racket的更为惯用和简短的相同代码版本:

(define (towers-of-hanoi n source dest temp)
  (cond [(= n 1)
         (printf "Move the disk from ~a to ~a~n" source dest)]
        [else
         (towers-of-hanoi (sub1 n) source temp dest)
         (printf "Move the disk from ~a to ~a~n" source dest)
         (towers-of-hanoi (sub1 n) temp dest source)]))
无论哪种方式,它都能按预期工作:

(towers-of-hanoi 3 "source" "dest" "temp")

Move the disk from source to dest
Move the disk from source to temp
Move the disk from dest to temp
Move the disk from source to dest
Move the disk from temp to source
Move the disk from temp to dest
Move the disk from source to dest

在您的代码中,最后一个递归调用似乎是错误的,并且过程参数的顺序有问题。请尝试以下方法:

(define (towers-of-hanoi n source dest temp)
  (if (= n 1)
      (begin 
        (display "Move the disk from ")
        (display source) 
        (display " to " )
        (display dest)
        (newline))
      (begin 
        (towers-of-hanoi (- n 1) source temp dest)
        (display "Move the disk from ") 
        (display source)
        (display " to ")
        (display dest)
        (newline)
        (towers-of-hanoi (- n 1) temp dest source))))
我注意到你一直在问被标记为
racket
的问题,这里有一个关于racket的更为惯用和简短的相同代码版本:

(define (towers-of-hanoi n source dest temp)
  (cond [(= n 1)
         (printf "Move the disk from ~a to ~a~n" source dest)]
        [else
         (towers-of-hanoi (sub1 n) source temp dest)
         (printf "Move the disk from ~a to ~a~n" source dest)
         (towers-of-hanoi (sub1 n) temp dest source)]))
无论哪种方式,它都能按预期工作:

(towers-of-hanoi 3 "source" "dest" "temp")

Move the disk from source to dest
Move the disk from source to temp
Move the disk from dest to temp
Move the disk from source to dest
Move the disk from temp to source
Move the disk from temp to dest
Move the disk from source to dest

我通过自己的研究和本网站关于河内塔的其他帖子,找到了以下关于河内塔的有用页面:我通过自己的研究和本网站关于河内塔的其他帖子,找到了以下关于河内塔的有用页面: