kernelthread.com

FORTH

Charles Moore

The Towers of Hanoi as a FORTH program.

According to Charles "Chuck" Moore (picture on the right), creator of the language, Forth is so called because:

"The first time I combined the ideas I had been developing into a single entity, I was working on an IBM 1130, a "third-generation" computer. The result seemed so powerful that I considered it a "fourth generation computer language." I would have called it Fourth, except that the 1130 permitted only five-character identifiers. So Fourth became Forth, a nicer play on words anyway."

More information on FORTH can be found at www.forth.com and www.forth.org

( The Towers Of Hanoi ) ( FORTH ) ( Copyright (C) 1998 Amit Singh. All Rights Reserved. ) ( http://hanoi.kernelthread.com ) ( ) ( Tested under GNU Forth 0.3.0, PFE 0.9.14 ) ( Use "gforth <thisfile> -e 'n HANOI bye'" to run ) ( hanoi with n disks. Alternatively, load everything ) ( and use the HANOI word from within the interpreter. ) : MOVEIT ." move " . ." --> " . CR ; ( to from -- ) : DOHANOI ( to from using n -- ) ( T3 <- T1 using T2 ) DUP 0 > ( more disks ? ) IF 1 - ( n <- n - 1 ) 2OVER 2OVER ( clone data stack ) >r >r >r >r ( save it to rstack ) 1 ROLL 2 ROLL 3 ROLL 3 ROLL ( using from to n-1 ) RECURSE ( T2 <- T1 using T3 ) 2r@ SWAP MOVEIT ( to from ) 2DROP 2DROP ( empty the stack ) 2r> 2r> ( from to n-1 using ) SWAP ( from to using n-1 ) 3 ROLL ( to using n-1 from ) SWAP ( to using from n-1 ) RECURSE ( T3 <- T2 using T1 ) THEN ; : HANOI ( n -- ) ( prepare arguments ) 3 1 2 3 ROLL DOHANOI 2DROP 2DROP ;

Download

hanoi.4th