Homework 01

UPenn CIS 194 Spring 2013

Homework 1

The problem statement is here. The interesting problem is the classic Tower of Hanoi puzzle. Using Haskell, this problem was reduce to very compact representation of the recursive logic itself:

type Peg = String
type Move = (Peg, Peg)
hanoi :: Integer -> Peg -> Peg -> Peg -> [Move]
hanoi 0 _ _ _ = []
hanoi 1 x y z = (x, y):[]
hanoi n x y z = (hanoi (n-1) x z y) ++ (hanoi 1 x y z) ++ (hanoi (n-1) z y x)

So elegant and simple once I get my head around the types and how to build up a list.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s