Sunday, January 24, 2010

A Little Bit of Clojure and Scheme

Following my desire to read as much SICP as I can without going nuts, I did at least one SICP exercise (Exercise 1.8) with both Scheme and Clojure.

I'm putting them here for a lightweight comparison. They're not supposed to be efficient or clever solutions, just the first ones I came up with. Since there are really few differences between the two languages at this level, I took the liberty of using Clojure thread-first macro to be able to contrast something. Thanks to Sean Devlin screencast on the subject.

Scheme
(define (square x)
     (* x x))
(define cube
     (lambda (x)
     (* x x x)))
(define (cubert x)
   (cubert-iter 1 x))
(define (cubert-iter guess x)
   (if (good-enough? guess x)
       guess
       (cubert-iter (improve guess x)
                    x)))
(define (good-enough? guess x)
   (< (abs (- (cube guess) x))
          .0001))
(define (improve y x)
   (/ (+ (/ x
            (square y))
         (* 2.0 y))
       3.0))

Clojure
(defn square [x]
     (* x x)) ;I prefer fn than using the #() macro
 (def cube
   (fn [x]
    (* x x x)))
  (defn cubert [x]
   (cubert-iter 1 x)) ;Need to reorder before usage or "declare" beforehand
  (def good-enough?)
  (def improve)
  (defn cubert-iter [guess x]
   (if (good-enough? guess x)
       guess
       (cubert-iter (improve guess x)
                  x)))
  (defn good-enough? [guess x]
   (< (Math/abs (- (cube guess) x))
          0.0001)) ;Uses the thread-first macro to make it a bit different
 (defn improve [y x]
   (-> x
     (/ (square y))
     (+ (* 2.0 y))
     (/ 3.0)))

Saturday, January 9, 2010

Clojure, Cliché and Nerdiness

More than a year ago, I posted about my dabbling in Scala, and wanting to check out Haskell and Clojure.

Contrary to my expectations, I ended up being attracted to Clojure the most. I do read up a lot about it. Beside the excellent Programming Clojure, I read the Clojure Google group and various blogs. I bought the screencast Functional Programming with Clojure, watched the various screencasts by Rich Hickey and listened several times to the NYC Lisp 2007 presentation. For those interested, here is where to start with Clojure.

My problem though, as always when I try to learn a language, is too much reading and not enough coding!

In what seems to have become a cliché, I'm now watching the famous SICP lectures and going through part of the book. I'm hoping I will do some of the exercices 8) I'm using PLT Scheme with the SICP support.

Anyway, watching the lectures, I was suddenly hit by something: "I'm such a nerd to be enjoying this so much!"

I'm a professional programmer, but I'm beginning to think I'm really a hobbyist programmer at heart. I learned to program reading a introductory column in a newspaper using pen and paper. Later my parents got me a Commodore 64 and I was hooked. I predictably graduated in computer science. I had a course given with the first edition of the SICP book. Unfortunately back then, I really did not appreciate it or Lisp.

So here I am, 15 years down the road, and I'm revisiting my computer roots to see what I missed the first time around.