I have been silent in the blogosphere for the past few months. Life continues to be very busy. The SICP study group is progressing well. I am having a lot of fun interacting with Tom, Pradip, Martin and others. I am close to finishing chapter 2. Almost all the exercises have been completed and tons of things learnt. I wonder how some people find time to do the problems and blog about them.

I also switched to Scheme from Clojure as my language for doing SICP exersizes for many reasons. One important reason is the fact that the purpose of the whole thing is to do SICP and general principles of Computation. I find Scheme to be an excellent vehicle for that purpose. Thinking back, I think it was emacs + slime which was holding me back from trying other alternatives. :-)

And then it happened. I bought ”The Little Schemer” by Dan Friedman and Matthias Felliesen and simply fell in love with it. In my opinion, this little book is a much more important book in computing than the mighty Knuth and K&R. True, they have their place. But this book conveys some of the most important principles in computation in a simple Q&A style. I had a smooth sail through it until I hit the chapter on Y Combinator (aptly titled ”… and again, and again, and again .. “) which I had to read about 4 times and experiment on the repl to finally get it. Reading this book also motivated me to take a serious look at Scheme. I managed to grok some of the fine papers written by Dybvig, Felliesen and the other fine and friendly folks at the PLT. The PLT community and the mailing lists are one of the finest and helpful group of folks I have seen.

I am currently looking at Guile, the FSF’s own Scheme implementation. There are many factors which makes guile interesting. Guile is small, but at the same time good enough to be very useful to write large systems. It has a good C FFI and has a good module system, something I haven’t seen in the other Scheme implementation and is the closest thing to Common Lisp packages, in my opinion. It has a nice VM and a compiler, which is evolving. I hope I can learn a lot more by working with Guile than any other Scheme. Guile is actively being developed by Andy Wingo and others who can also be interacted with at the #guile irc channel, which is great. I use Emacs and geiser to interact with Guile.

Clojure was fun. I will return back to it when I have a need. I almost finished writing a blog engine with it and had a lot of fun writing it. But for now, Scheme definitely serves my needs for learning programming language fundamentals.

At this point, some people in the Clojure community are busy trying to get all the attention they can (and also sell some books, more in the works, so we will see a proliferation of suchposts for the next many months) by posting every blog post they find on clojure to HN, Reddit and also repost the HN url back to twitter! The result of all these is that, a lot of people think Clojure is the first Lisp in this planet! I mean.. grow up, people! I am sure, they will settle down. Can’t resist posting this XKCD strip. But overall, over exposure of Clojure is good for all the Lisps.

I have a bunch of books waiting to be read or partially read:

On Lisp (almost finished it, but planning to read again).

The Lambda papers by Steele and Sussman.

To Mock a mockingbird.

PLAI (Shriram Krishnamurthi)

EOPL

PAIP

The Joy of Clojure.

...

Not sure whether a lifetime is enough to read and understand them all and also make something useful for myself and others to use.

I now have a very nicely working swank-clojure, completely configurable by hand and no ELPA magic. And this plays very well with the upstream slime as well as swank-clojure, albeit some small changes in the emacs lisp of the swank side..

The problem

Upstream swank-clojure supports only ELPA and those who want to configure swank manually are on their own. I, for one, do not like those “conventions over configurations” stuff, atleast for my programming environment. Heck, that’s why I use emacs in the first place.

In the past, I was using an older version of swank-clojure which fully supported manual configuration. I then switched to a newer version of swank-clojure but with a totally different emacs lisp code to make configuration for easy.

Also, I use the clojure and clojure-contrib from the debian packages, which installs into /usr/share/java. The same directory has all the other java jars as well. But upstream swank-clojure supported somewhat different directory structure. If invoked on a project, it picks up dependencies from a subdirectory called “lib” (configurable). lein puts all the dependencies (including clojure.jar) into that directory. It built up a new classpath from these (assuming that the project is self-contained, including dependencies).

This unfortunately does not work in my case. I almost never use a build tool while fooling around doing random with slime. For example, to use Slime with my SICP exercises where I do not have a build mechanism put in, it becomes difficult. Running tests for a namespace from slime is another reason why having the project namespace in the classpath is a very useful thing. For many simple projects that I do, all I need is clojure and clojure-contrib which is already in my classpath. But this made things difficult as adding the project being worked on into the classpath was messy. Becausee of the above setup, swank-clojure-project was unsable for me..

I also don’t like multiple copies of jars lying around. It is not the question of wasting disk space, which is very cheap anyway. But it is just a matter of taste and cleanliness. Multiple jars on each project directory is a convenience, but it is plain ugly.

Today I decided to clean up the whole mess. The end result is a much cleaner swank/slime.

Solution

Turned out the solution was very simple. When swank is started on a project, instead of changing swank-clojure-classpath, I build up another list using a variable called swank-clojure-extra-classpaths. This is then appended to the swank-clojure-classpath before starting up the JVM.

I also disabled the code which checks the presence of clojure.jar files in some predefined location when swank starts up. This can always be called manually, if one wants to.

Code

For those wanting to follow similar approach, here is my github fork of swank-clojure with instructions on setting it up. The emacs repl (via ielm) was invaluable for debugging.

After several failed attempt to understand maven and going back and forth between lein and maven, I have finally decided to settle on maven. There are several reasons for this. One of the most important one is that Debian has a very well packaged maven and a local repository system. i.e. Debian java packages installed on a machine is available as a local repo. With that, offline builds become quite easy (provided all the dependencies are available as Debian packages). Maven is also well supported by the Java ecosystem. Though XML turns me off as well, maven takes care of generating much of the boilerplate for us.

I will walk through creating a trivial clojure project using maven. I assume that maven is already installed on your machine.

A directory called hello is created. Let us look at the parameters to archetype:create. groupId is a unique way to identify the module. Think of it as the namespace. artifactId is the name of the project. groupId:artifactId:version uniquely identify an ‘artifact’. By default, maven will create a version number 1.0-SNAPSHOT for the project, if it is omitted from commandline.

Let us get rid of the java files first.
$ rm -rf src/main/java
$ rm -rf src/test
Create the following directory structure:
$ mkdir -p src/main/clojure/in/clj
Note that we have created the structure according to our groupId specification. Create a file called hello.clj at the leaf of this directory and put the following contents inside it:

REXML could not parse this XML/HTML:
<pre>
(ns in.clj.hello)

(defn greet & args (apply str args)) </pre>

Now, open the pom.xml file. Some changes needs to be done here so that maven knows that we are building a clojure project and the dependencies. The following things are done:

A jar file is created under the target directory. Now, we add the jar into our classpath and invoke the clojure repl:
$ java -cp /usr/share/java/clojure.jar:target/hello-1.0-SNAPSHOT.jar clojure.main
Clojure 1.1.0
user=> (use 'in.clj.hello)
nil
user=> (greet "Hello World")
"Hello World"
user=>
You can also invoke the repl from maven:
$ mvn clojure:repl
That wasn’t really too hard. With emerging tools around maven like the Polyglot Maven which supports Clojure and a S-expression syntax for writing the POM, we don’t even have to do as much as we did now. I have put this example in this repository for anyone to play with.

This book is not strictly in the category of “self help” books meant to give a temporal high. It is a set of advice (54 of them) for any programmer to improve himself. Each of the chapter is only 2 or 3 pages long, so it is quite easy to read and digest. I have put some of my notes on the book in this wiki page. The author had earlier lived in India, apparently managing an outsourced team here and makes some good and correct observations on the general attitudes of Indian software programmers.

The book is worth a read. If you have no time to read the entire book and just need a summary, read my notes.

Now, you want to have the name and age of people in this list of those with the age 27. Clojure’s comp function comes to the rescue. comp takes some functions and applies them to the data from right to left. i.e., It takes the right most function, applies it to the input parameters, the result of which goes as input to the next right most function and so on. Let us take one single map and pass it to the comp:

user> ((comp #{33} :age) {:name "Ram" :age 33})
33

What this does is, it takes the input map, applies the :age function on the map (Clojure’s hashmap keys are functions themselves). The result of this is used to see if it falls in the given set (which just has 33). Now, sets are functions of their members. Here is an example:

I finished reading ”Pragmatic thinking and learning” by Andy Hunt (who wrote Pragmatic Programmer and other books). If one is really interested in self-improvement, this is the book to grab. For me, most things described in the book were known things, but it got very reinforced because Andy has some magical power to convey ideas in a very convincing way.I highly recommend this book for anyone interested to know more about learning and apply those things into their own daily lives. The book explains the process of learning using the Dreyfus model and how one advances from a beginner to an expert. Ever wondered why you get new ideas while in the bathroom? Have you noticed why you suddenly perform better when you are in the middle of good performers? I have many times noticed that I speak better english when I am talking to a good english speaker and vice versa. This book explains why, using a simplified model of the human brain.

One noteworthy suggestion in the book for every reader is to keep a personal wiki, which I had experimented with, sometime last year, with great success. I especially liked the meditation technique described in the book and can say with some confidence that it works very well. The chapter on “focus” is also very well researched. The book says that each context switch costs about 20 minutes. Interestingly, the book does not talk about the concept of flow.

I greatly enjoyed reading “Pragmatic Programmer: From Journeyman to Master” earlier (despite the extremely bad packing from Flipkart, which made the paper curly, sort of permanantly).

Doesn’t, the unix commands find, xargs and grep look like map, apply and filter of the functional world? I was looking at the OCW website and downloaded some content from there as a zip file, which had a complicated directory structure. I wanted to flatten the directory structure, so that all the pdfs are in one directory. With find and xargs it turned out to be extremely easy.

$ find ./ -name *.pdf -print | xargs -I xxx cp xxx /tmp

The key is the -I argument to xargs which replaces every occurance of the pattern in the command with the input from stdin.

Also don’t the pipes look like calling a function composition like in f(g(x) as mentioned by this post?

So, here I am, (re)starting the SICP from today. Hopefully this time I will complete the book. I am very tempted to do the exercises in clojure, so that I won’t copy the code as it is from the text. I am not sure whether future chapters can become difficult for me because of this, but I think I will give it a try with clojure. Also I believe I will need only a subset of clojure so that use only the fundamentals and no advanced facilities.

Some of us in the bangalore-fp group have decided to form a study group on SICP. The main reason behind study groups is that it is a great way to learn any new subject or a literary work. People share what they read, ask questions, have discussions around these questions they ask etc etc. Also regular meetups provide a time tick to the group and is very motivational. If anyone is interested, please contact me or subscribe to bangalore-fp. We will most likely have a meetup this sunday. The venue and time is not yet decided. I will update this post once it is agreed upon. I am not sure whether this will be a “success”. I don’t even know how to measure success (in general, not just in this context). If one person manage to get inspired to complete the entire book, then I guess it can be considered as a success. Update: The meeting is at this place called Jaaga on sunday, 14th March 2010 from 3pm.

Common folklore (confirmed by some Norwegian friends) is that heat is not a friend of laptop batteries. The life of a laptop battery tend to reduce in Indian weather. One of the Dell Laptops I bought in early 2008 can now work barely 30 minutes with a fully charged battery.

Linux kernel provides a wealth of information (through ACPI) about the Battery like the make, type, serial number etc. Look under /sys/class/power_supply/BAT0/. This is what I have:
~$ cat /sys/class/power_supply/BAT0/status
Unknown
~$ cat /sys/class/power_supply/BAT0/technology
Li-ion
~$ cat /sys/class/power_supply/BAT0/type
Battery
~$ cat /sys/class/power_supply/BAT0/serial_number
32511
~$ cat /sys/class/power_supply/BAT0/manufacturer
SANYO
~$ cat /sys/class/power_supply/BAT0/model_name
42T4511
~$ cat /sys/class/power_supply/BAT0/energy_full_design
84240000
~$ cat /sys/class/power_supply/BAT0/energy_full
64530000
~$ cat /sys/class/power_supply/BAT0/energy_now
63160000
It looks like, my battery (this is a work laptop, Lenovo Thinkpad T61 running Debian x86-64 unstable) is functioning at a lesser capacity than it is designed. It will be nice to corelate these readings with the actual performance of the battery. I tend to run this machine mostly with AC, but I will try profiling it for battery performance and see how much time these numbers translate to.