Friday, August 18, 2006

Eager Comprehensions for Black Belts - An SRFI 42 Tutorial

Everything Scheme: Eager Comprehensions for Black Belts - An SRFI 42 Tutorial

Eager comprehension sounds like a really complicated and obscure principal. It turns out, however, it couldn't be more basic. It all boils down to generating a sequence of some kind, and then iterating over it.

In the typical spirit of Scheme, the language spec barely includes a looping construct. However, using the macro facility and tail recursion, you can develop really sophisticated libraries for loops - and this is one of them.

This tutorial walks you through the process of creating and working sequences (of numbers, characters, etc.) that end up being really terse. Such as:

(require (lib "42.ss" "srfi"))
(list-ec (: i 5)
        (* i 2))
; => (0 2 4 6 8)

Here, list-ec created a sequence from 0 to 4 and multiplied each element by two.

It's cool stuff, and it demonstrates how you can take a language with a tiny core, and build on top of it elegant abstractions.

I sure hope Jens Axel Søgaard keeps on publishing goodies like this.

No comments:

Post a Comment