Brain
The Towers of Hanoi as a Brain language program.
Brain is a high-level, purely object-oriented, prototype based scripting language, mostly similar to the Self language. Some features of Brain are:
- Everything is an object
- Arbitrary precision numbers (using the gmp library)
- Built-in high-level data structures
- Automatic memory management (using a mark and sweep collector)
- Closures
- Exceptions
-- The Towers Of Hanoi
-- Brain Implementation
-- Copyright (C) 2003 Amit Singh. All Rights Reserved.
-- http://hanoi.kernelthread.com
--
-- Tested under Brain 0.5.2 (http://brain.sourceforge.net)
--
movedisk =
{
|f t| -- (from, to)
--
f print.
" --> " print.
t println.
}.
dohanoi =
{
|n f u t| -- (n, from, using, to)
--
(n == 1)
if-true: {
movedisk(f, t).
}
if-false: {
dohanoi(n - 1, f, t, u).
movedisk(f, t).
dohanoi(n - 1, u, f, t).
}
}.
hanoi =
{
|n| -- (n)
--
(n < 1)
if-true: {
"hanoi: number of disks must be a positive non-zero integer" println.
brain exit.
}
if-false: {
-- nothing
}.
(n > 10)
if-true: {
"hanoi: number of disks must be less than 10" println.
brain exit.
}
if-false: {
-- nothing
}.
dohanoi(n, 1, 2, 3).
}.
hanoi(11).