How To Organize a Picnic on a Computer

Ok, so here's what the program is going to do... On the website, people answer a bunch of random questions. What we want our program to do is take a picture of a city park, a list of answers people gave on the questions, and have it figure out where people should sit in the park so that their neighbors are as similar as possible to them, based on similar answers for the questions. We're going to do this using a standard simulated annealing algorithm. (Read this for more info... we'll be describing the basics below, too...)

Installing GHC Haskell On Your Computer

The only preparation you need to do for this tutorial is install the Glasgow Haskell Compiler- You can get the latest version from here. It's a very well supported and very fast compiler. We're just going to use the basic GHC libraries in this tutorial- No other installs are needed.

Hello World! Let's Have a Picnic!

Now you have all you need to run the "Hello World" program below- Just copy the code into a file called tutorial.hs:

import Data.List
import Text.Regex
import System.Random
import Data.Ord

type Point     = (Float,Float)
type Color     = (Int,Int,Int)
type Polygon   = [Point]
type Person    = [Int]
type Link      = [Point]
type Placement = [(Point,Person)]

type EnergyFunction a              = a -> Int
type TemperatureFunction           = Int -> Int -> Float
type TransitionProbabilityFunction = Int -> Int -> Float -> Float
type MotionFunction a              = StdGen -> a -> (StdGen,a)

main = do 
  putStr "Hello World! Let's have a picnic! \n"

After you've copied the code to a file, just run your new program from the command line as shown below:

> runHaskell tutorial.hs

Hello World! Let's have a picnic!

*phew* That was easy!

How Does This Code Work?

As you can see, there's lots of stuff in this "Hello World" that doesn't seem necessary... Actually, only the last two lines (starting at main = do) are really needed to print our message... The rest is just header stuff that we'll use later in the tutorial...

At the top, you can see some things we're importing out of the standard GHC libraries:
  1. The Data.List module has extra functions for slicing and dicing lists- Lists are fundamental in Haskell programming and we'll be using them a lot in this program.
  2. The Text.Regex module let's us do funky things with text strings using regular expressions- If you don't know what this means, read this tutorial first. Every programmer should know about regular expressions.
  3. The System.Random module, as you've probably guessed, lets us use random numbers, essential for simulated annealing.
  4. The Data.Ord just contains a single function I'm really fond of, called comparing. I'm not happy if I can't use comparing, and I think we all want this to be a happy tutorial...

The next few lines define different types that we're going to use in our picnic program... Haskell programmers are really fond of lots of types, because Haskell compilers are basically just these crazy type-crunching machines that will take any types you give them and if they find anything questionable about how you're using types they will let you know early, preventing your program from becoming buggy. The way Haskell compilers handle types puts them head and shoulders above most other compilers (Read more about Hindley-Milner type inference here.)
The first few type lines should be pretty self-explanatory, for a program that's going to work with pictures: A Point is just a pair of floating numbers, a Color is an rgb triple, a Polygon is just a list of Points, a Person is just a list of the answers they gave in the questionaire- Very simple stuff...

The lower type lines are a bit more difficult to understand- They describe the types of the four functions that are used in all simulated annealing algorithms... we'll be describing those later on. For now, just know that in Haskell you want to look at the last arrow, which looks like this ->... Anything to the left of that arrow is a parameter into our function- The thing to the right is the return value of the function. For instance, the EnergyFunction is a function that takes an arbitrary type a (which will be a seating arrangement of picnickers) and returns an integer, which is the "energy" of our picnic. A low energy will mean that all the picnickers are happy with where they're sitting. We'll explain these funcitons more later.

What's Good about this Code?

What's good is that in Haskell we can create all kinds of types to describe what we want to do and the Haskell compiler is very good at catching potential errors for us ahead of time. Defining types in Haskell is usually a choice and not a necessity- This program would run just fine, even if all the type declarations were missing... The Haskell compiler would just figure them out on its own!

Another thing that's good is that the part that writes "Hello World" is very short and simple in Haskell.

What's Bad about this Code?

To keep things simple I'm building these new types using the type command, instead of another command called data. This other command (which you don't need to know about today) lets you attach a special new type name that you use to construct your new type- That name makes it even harder to accidentally use the wrong type in the wrong place in your program.

Another thing thats "bad" about this code is that we used the type Int. Unlike all other types in this program, Int has an upper limit and could theoretically cause our program to err out if any integers got way too massively big. In Haskell, this type of integer can't get bigger than 2^31... We could have used the type Integer instead- This type of integer grows "magically" and can hold any sized integer, no matter how big... but it's slower, so we used Int instead, since our numbers will never get that big.