martes, 29 de diciembre de 2009

Binary search and dumb compression in Common Lisp

In my quest to learn lisp, I've been alternating scheme and common lisp readings and lectures.

I find scheme really enlightening, but quite difficult to write in it. Maybe it's because I haven't officially reached the set! chapter in SICP, and I still find difficult to write (so) functional code.

The point is that I finished this book and then, I started Paul Graham's ANSI Common Lisp. I've been really enjoying this reading, because it shows you interesting things (from an experienced programmer point of view) from the very beginning.

During the first 3 or 4 chapters, threre are some code examples, that I've tried to write on my own (maybe grasping a bit on graham's code when I got stuck, but well)

First, here's a compress/decompress lists lisp code. When entered a list like (0 0 0 1 2 1 1 0 4 4), compress returns a list of atoms or lists themselves, that contain the number of repetitions and the value to repeat: ((3 0) 1 2 (2 1) 0 (2 4)) . It's nice to see that at such low level point, being able to ask for a type of a given element gives us enough power to code this in a very simple way. I don't even want to think how I'd code it in java/c++.

Decompress does obviously the reverse operation

The binary search is just what its name says. Does a binary recursive search on an already sorted vector (array of 1 x N dimension).

I'm getting very fond of 'cond'. It takes the best things of both if and switch/case worlds. A switch-case like syntax, with powerful conditionals (not just comparing with '=')

That's all for now. Sorry for the crappy gists code listings. it seems github doesn't know much about common lisp (syntax nor indenting).

Btw, I've received my SICP copy in dead-tree format, so I'll probably spend some time with scheme when I finish Ansi Common Lisp.

No hay comentarios: