CHICKEN Scheme: Three Great Eggs
I was reading through Eggs Unlimited and was struck by just how much cool stuff is in there since the last time I checked. CHICKEN has an impressive collection of extensions, and reading about them sets my synapses firing with ideas for things to write. In this article, I would like to focus on three extensions that I feel every CHICKEN hacker should check out. They are extensions that increasingly appear in almost every program I write, but are perhaps not as well known as they deserve.
Before I discovered the fmt egg, output-heavy programs of mine were littered with printf, sprintf, print, and newline calls, and the ubiquitous formatting strings associated with those first two, plus plenty of ad hoc string formatting code of my own. There is a format egg, and a format-modular egg that both provide a Common Lisp style format procedure, but to use them involves using a complicated template mini-language.
The fmt egg provides an alternative that seems more schemely. It provides the fmt procedure, which like Common Lisp's format takes an output spec as its first argument — #f for a string, #t for the current output port, or a port. The remaining arguments (as many as you like) are objects and format procedures. Objects are formatted as display would do. Format procedures are called, and the egg provides a capable set of formatters and combinators to do everything the template-based formatters can do, but with simpler and more readable code.
Matchable provides a set of
syntax forms for pattern matching that can simplify just about any program
that uses lists, which let's face it, is most Lisp programs. Importantly,
its match-lambda and match-lambda* forms make procedures with pattern
matching of the argument list with a level of sophistication above that
provided by case-lambda. These have become go-to forms for me in
solving problems with list data to such an extent that whenever I start a
(use matchable) is among the first things I write.
Behavior-driven development first came to my attention with the release of the Buttercup test framework for Emacs. I was impressed with the readability of the tests as well as the test output, compared to other test frameworks I had used, and I was thinking about writing something similar for CHICKEN, when I found that happily, it already existed: Missbehave.
Missbehave does not plug in
chicken-install -test facility like the test egg, but instead
it provides its own invocation program,
behave. You write a script
containing your tests and run
behave on it, which runs your tests and
formats their results in green and red. Using a separate program to test
instead of chicken-install has the advantage that you can arrange to test
your project without installing it. I setup my tests.scm like this for
a recent project:
(module svgvla-defs *
(import chicken scheme)
;; ... tests ...
The file svgvla-defs.scm contained all of the code of my project, not in a module. This allowed me to wrap it in a module in my test file, and export everything I wanted to test, in this case everything.
If you're a CHICKEN hacker and haven't heard about these eggs, check them out, and if you haven't looked through Eggs Unlimited for a while, there is a lot of cool stuff in there. Some eggs that have caught my interest lately include comparse, cells, hypergiant, monad, yasos; maybe the subject of a future blog post. Happy hacking.