I’ve only done some tinkering in Clojure. My first real Lisp style exposure was doing a tiny bit of Scheme in college. About two years ago I read Practical Common Lisp, but didn’t really do much. When I discovered Clojure (which let me use all my existing knowledge of the Java libraries) I started tinkering.
Below are the programs that I’ve created so far.
My Ants
I first learned about Clojure from a great talk given by the language’s creator that explained not only the decisions he made, but in which he made a very impressive parallel simulation of an ant colony. The code for the simulation is posted on the web (on Google Groups).
So I took that code and started to make little modifications to it so I could get some experience under my belt. I loved the program, but wanted to see if I could make the ants seem a little more intelligent and a little less random. If you my version, called MyAnts.clj, you’ll see:
- I altered the cell structure to contain the cell’s XY coordinates
- I added a function called distance-to-home that uses the coords to figure out how far a cell is from the home area
- Which is used to make the ants prefer to move towards home thanks to the distance-to-home function
- Made the ants have a slight preference to moving in a straight line (so they’d stop turning so much)
- Gave the ants heads when drawn so I could tell which direction they were facing in
- Made a function called update-scale that (in real time, neat!) would scale the graphics up or down to let me better fit the window on my screen
- I also gave the window a title
I started this tinkering about February 23rd of 2009, and last updated it on March 5th.
Guess
My next program was written on February 26th. I wanted to try doing something that you can only easily do in languages like Lisp where code is data. I decided that a number guessing game would be a simple program. You enter a number, and it runs a recursive binary search, building a list of code (really just a + operator and a bunch of 1s) until that generated code returns the right value. It’s not the prettiest and it’s definitely not an efficient number guessing game (thanks to the code-as-data requirement I imposed), but it was a great learning project. You can download Guess.clj.
I wrote this program before finding Mark Volkmann’s fantastic Clojure – Functional Programming for the JVM, which really surpasses the main documentation on Clojure.org. After reading that I’ve found quite a few things I didn’t understand right and things I could enhance. Because of this, some of the features I wrote and code I used is unnecessary thanks to built-in features I didn’t use. This program, as well as Primes, could both use some enhancement.
Primes
My third program in Clojure was written on March 5th and 6th of March. It was a program to determine if a given number is prime or not. It’s known, simply enough, as Primes.clj. Some of the features of the program are:
- The program starts with only the number 2 as prime
- When asked if a number is prime it recursively builds up the prime list until it knows the right answer
- Since it keeps the list, it effectively caches answers.
Sierpinski
This little program, called Chaos Game, generates a Sierpinksi’s Triangle using an iterative method called the chaos game. I thought of a way to make it recursive, but haven’t bothered to do it. There is an example of how to run it at the bottom of the file.
It is generated at 800×800 pixels, which can be changed by editing a define near the top of the file. The three points used can be edited as well. It just runs forever, so once things show well you’ll either want to kill it or define running to be false, which will cause it to stop.
I was able to do this last Friday, May 1st, 2009 in under two hours, which included debugging the double buffered drawing and setting up correct threads to do the work. I think I’m really starting to get Clojure.
