lambdaspeech
::
hanoi
1
[
pages
][
login
][
load
]
_img https://upload.wikimedia.org/wikipedia/commons/thumb/0/07/Tower_of_Hanoi.jpeg/260px-Tower_of_Hanoi.jpeg _h1 towers of hanoï {pre A B C A2B A B C A2C A B C B2C A B C A2B [- | ] . [ | ] . [ | ] . [ | ] [-- | ] . [-- | ] . [ | ] . [ | ]- [--- | ] . [--- |- ] . [--- |- ]-- . [--- | ]-- A B C C2A A B C C2B A B C A2B A B C [ | ] . [ | ] . [ | ] . [ |- ] [ | ]- . [ | ] . [ |-- ] . [ |-- ] [ |--- ]-- . [- |--- ]-- . [- |--- ] . [ |--- ] } _h2 1) '{lambda speech} {pre {@ style="box-shadow:0 0 8px #000"} '{def move {lambda {:n :from :to :via} {if {<= :n 0} then > else {move {- :n 1} :from :via :to} move disk from :from to :to {br} {move {- :n 1} :via :to :from} }}} -> {def move {lambda {:n :from :to :via} {if {<= :n 0} then > else {move {- :n 1} :from :via :to} move disk from :from to :to {br} {move {- :n 1} :via :to :from} }}} } {pre '{move 2 A B C} } {move 2 A B C} {pre '{move 3 A B C} } {move 3 A B C} {pre '{move 4 A B C} } {move 4 A B C} _h2 2) other languages _p Following [[https://rosettacode.org/wiki/Towers_of_Hanoi|https://rosettacode.org/wiki/Towers_of_Hanoi]] _h3 newLisp {pre (define (move n from to via) (if (> n 0) (move (- n 1) from via to (print "move disk from pole " from " to pole " to "\n") (move (- n 1) via to from)))) (move 3 1 2 3) } _h3 picoLisp {pre (de move (N A B C) (unless (=0 N) (move (dec N) A C B) (println 'Move 'disk 'from A 'to B) (move (dec N) C B A) ) ) (move 3 'left 'center 'right) } _h3 Racket {pre #lang racket (define (hanoi n a b c) (when (> n 0) (hanoi (- n 1) a c b) (printf "Move ~a to ~a\n" a b) (hanoi (- n 1) c b a))) (hanoi 3 'left 'middle 'right) } _h3 Scheme {pre (define (hanoi n a b c) (if (> n 0) (begin (hanoi (- n 1) a c b) (display "Move disk from pole ") (display a) (display " to pole ") (display b) (newline) (hanoi (- n 1) c b a)))) (hanoi 3 1 2 3) } _h3 Python {pre def hanoi(ndisks, startPeg=1, endPeg=3): if ndisks: hanoi(ndisks-1, startPeg, 6-startPeg-endPeg) print "Move disk %d from peg %d to peg %d" % (ndisks, startPeg, endPeg) hanoi(ndisks-1, 6-startPeg-endPeg, endPeg) hanoi(ndisks=4) } _h3 C {pre #include
void move(int n, int from, int to, int via) '{ if (n > 0) { move(n - 1, from, via, to); printf("Move disk from pole %d to pole %d\n", from, to); move(n - 1, via, to, from); } } int main() '{ move(4, 1,2,3); return 0; } }
lambdaspeech v.20180812