Pages
Archives
- September 2011
- April 2011
- February 2011
- January 2011
- November 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- February 2010
- January 2010
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
- August 2006
- July 2006
- May 2006
- March 2006
- February 2006
- January 2006
- December 2005
- October 2005
- September 2005
- August 2005
- May 2004
- March 2002
- October 2001
Side by side: Python, Common Lisp, Clojure
UPDATE: Find the rest of the code at http://bitbucket.org/gavinmcgovern/clj-bayes/.
My holiday project (of the sort that doesn’t involve cooking at least) is porting the Bayesian spam code from Peter Seibel’s great book “Practical Common Lisp” to Clojure. This will be a big part of the next-gen Big In Twitter that’s slowly coming together. Although I’ve been using Bayes, I haven’t really understood what was going on under the hood. I’m finding Peter’s chapter a fantastic walkthrough & approach to understanding it. He presents the basics and then goes on and adds optimizations. Perfect.
It was smooth sailing up until the other day. One little function tripped me up. I’ll show you.
Peter based some of his spam filter Common Lisp on a Python implementation from an article by Gary Robinson. Specifically Peter created a chi square function from this Python:
There’s a lot to like there (I removed the comments): concise, minimal noise, short. Even if you didn’t know the math (like me!) you could probably follow along. Would probably be even shorter if you used Python’s list comprehensions.
Here’s what Peter came up with for the Common Lisp version:
He uses the oddball loop macro. It’s a DSL for iteration. It’s charming, it’s weird, it doesn’t seem very Lispy. I like how it has synonyms, “summing” for “sum”, “collecting” for “collect”, etc. Verb tense agreement is important!
While there’s a loop in Clojure it isn’t at all related to Common Lisp’s loop. This is where things got a little muddy for me. Spent a lot of time trying various approaches and while I was able to achieve parts of the original function I wasn’t able to get the whole thing. The combination of “term *= m/i ” and the “sum += term” was killing me; so much happening at once.
Taking a breather I started poking around clojure-contrib. There is so much buried in there. A real gold mine. I eventually stumbled upon seq-utils and the “reductions” function. And that was exactly 100% what I needed. After Seq-utils and a little of Clojure’s list comprehensions and 10 minutes of coding I had this:
It’s been many many years since I did any sort of Common Lisp programming but one lasting memory was the vast quantity of high quality code freely available. Lots of motivated people writing excellent Common Lisp. I’m finding the same with the Clojure community. I love just being able to reach into the common libs, pull out a few gems and slap them together. Thanks! (Btw, anything wrong my version?!)