## The Towers of Hanoi in Erlang

I’m just playing with Erlang, below is my attempt at solving the Towers of Hanoi puzzle using it. Call it using hanoi:hanoi([1,2,3,4,5,6],[],[]) (a valid starting position is assumed).

```-module(hanoi).
-export([hanoi/3]).

hanoi(A,B,C) ->
Disk=lists:max(A),
move(Disk,A,B,C).

move(Disk,[Disk|Source],Dest,Temp)->
[Source,[Disk|Dest],Temp];

move(Disk, Source, Dest, Temp) ->
[S1,T1,D1]=move(Disk-1,Source,Temp,Dest),
[S2,D2,T2]=move(Disk,S1,D1,T1),
[T3,D3,S3]=move(Disk-1,T2,D2,S2),
[S3,D3,T3].
```

## Solving the Towers of Hanoi puzzle in C#

I thought this would be a fun little exercise to try. This puzzle is generally used to teach recursion in CS classes, but I have never actually tried to implement it.

```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TowersOfHanoiCs
{
class Program
{
static List peg1;
static List peg2;
static List peg3;

static void Main(string[] args)
{
peg1 = new List();
peg2 = new List();
peg3 = new List();

for (int i = 1; i < 8; i++)
{
}

Display();
MoveDisk(7, peg1, peg2, peg3);
}

static void MoveDisk(int disk, List source, List destination, List temp)
{
int position = source.IndexOf(disk);

if (position == 0)
{
source.Remove(disk);
destination.Insert(0, disk);
Display();
}
else
{
int nextDisk = source[position - 1];
MoveDisk(nextDisk, source, temp, destination);
MoveDisk(disk, source, destination, temp);
MoveDisk(nextDisk, temp, destination, source);
}
}

static void Display()
{
DisplayPeg(peg1);
DisplayPeg(peg2);
DisplayPeg(peg3);
Console.WriteLine("----------");
}

static void DisplayPeg(List peg)
{
foreach (int x in peg)
{
Console.Write(x + " ");
}
Console.WriteLine();
}
}
}

```

## Sending an email with syntax hightlighting for your source code

I resisted the switch to HTML e-mail for as long as I could, I know there are some who have managed to succeed in not switching to an HTML enabled email reader. But I just couldn’t hold out due to the fact that there were so many non-computer savvy people I communicated with who used it, and their e-mails were impossible to read without it, and I had to make the switch many years ago.

Since we’re all so used to reading code that has syntax highlighting in it, I like to have the source code contained in e-mails highlighted. Unfortunately, most syntax highlighters made for the web use CSS to style the code. And when you copy and paste the code into an email, the <style class=’…’> tags are copied, but not the actual style from the CSS, and what comes out on the other end is just text in the standard font. So the only way to get syntax highlighting in your email is to use a utility that embeds the styles into the HTML, rather than a CSS. I also wanted a utility that didn’t require me to save the source code and output as a file on my computer, creating a lot of unnecessary temp files that I’d probably never delete. So far, the only thing that actually fits the bill is this web based utility: http://tools.devshed.com/webmaster-tools/syntax-highlighting/

Basically, just paste your source code into the tiny little window, select the language, type the captcha, then cut and paste the result into your e-mail. It doesn’t have many options, and there’s lots of room for improvement, but it’s the best thing I’ve found so far.

Firing up ole’ TP7 for the last post put me in a nostalgic mood, and I went looking to see where Turbo Pascal stands today. It doesn’t look like you can still buy it, and the only version available is Turbo Pascal 5.5 which you can download for free from: http://edn.embarcadero.com/article/20803

I believe my first experience with Pascal was at Indiana University when I started my first CSCI class in 1990. I believe we were using version 5. I also remember the first time I fired up version 7 and saw that certain keywords where highlighted in different colors. I was so amazed I almost wanted to do cartwheels, I was so impressed with such an amazing idea. IDEs have come a long way since then, but the Lord only know how many hours I spent staring at the old blue and yellow screen of the Turbo Pascal DOS IDE.

Below are two screenshots from TP7. The first is the default layout of 80×25 chars with the default colors, the second is my custom layout with the amazing 80×50 char layout showing twice the number of lines! (Hey it was a big deal back in the day). The pop up box in the blue one shows a successful compile. I’m not sure where the program came from that’s shown in the second image, I’m pretty sure it’s not something I wrote, but it’s what popped up automatically when I started the IDE. I do remember I spent a lot of time playing with interfacing to Netware servers back then, this program appears to be an attempt to log in without using the API (which was just a series of INT calls). I think it’s either the reverse engineered source code that a fellow Netware “hacker” developed who I had the pleasure of exchanging many e-mails with: Willem Jan Hengeveled. If it’s not his code, it’s probably something based off of it. Now I think I’m going to have to install an old Netware server in a VM to play with.

## Statements in most languages can be empty

I was looking through some code that was posted for review when someone had pointed out a statement like the following:

```int x=5;;
```

Note the two semi-colons. I initially thought “Great, someone has us reviewing code they didn’t even try to compile.” But to my amazement, it does compile, and it compiles in pretty much every language. I’ve tested it on the newest and oldest stuff I have, the oldest/obscurest thing I could come up with was Borland Turbo Pascal 7 (the EXE has a date of 10/30/1992). I’m sure there’s some deep theoretic reason parsers need to accept it, but to a human, it looks like an obvious error.

## A really tough puzzle

100 prisoners are each locked in a room with three pirates, one of whom will walk the plank in the morning. Each prisoner has 10 bottles of wine, one of which has been poisoned. And each pirate has twelve coins, one of which is counterfeit and weighs either more or less than a genuine coin. In the room is a single switch which the prisoner may either leave as it is or flip. Before being led into the rooms, the prisoners are forced to wear either a red had or a blue hat. They can see all the other prisoner’s hats, but not their own. Meanwhile a six digit prime number of monkeys multiply until their digits reverse. Then all have to get across a river using a canoe that can hold at most two monkeys at a time. But half the monkeys always lie and the other half always tell the truth. Given that the nth prisoner knows that one of the monkeys doesn’t know that a priate doesn’t know product of two numbers between 1 and 100 without knowing that n+1th prisoner has flipped the switch in his room or not after having determined which bottle of wine was poisoned and what color his hat is. What is the solution to this puzzle?

This was played on the July 18, 2009 edition of Car Talk on NPR and attributed only to “Alan”. If you download the podcast, the puzzler starts at the 36:00 minute mark.