Levi's Lisp Log

Thoughts and links relating to Lisp and computer theory

The Art of Metaprogramming

A technique I've found very useful lately, both at work and in hobby programming, is metaprogramming. That is, using programs to generate other programs. At work, I generate a large amount of C data structure initialization code from a spreadsheet and a Python script. It makes updating the data trivial and integrates right into the build process, eliminating all sorts of possibilities for errors.

There's a series of articles on DeveloperWorks covering the Art of Metaprogramming; see part 1 and part 2. Of course, they introduce Scheme, which is certainly one of the best languages to use for metaprogramming. Enjoy!

# Posted by: Levi at 5:37 PM on Sunday, February 5, 2006
   Comments: (0) Categories: Programming Scheme C

Fun with FFI

I recently received a task at work to implement a command-line tool that provides an interactive shell interface to a network protocol. Since the GNU Readline library provides a nice, familiar interface for that sort of thing, I looked for a high-level language I was familiar with that had readline bindings.

After a bit of investigation, I was unable to find one that looked usable. I did, however, discover that Chicken Scheme had an "egg" for readline integration. It didn't have all the features I needed, but it looked easily extensible.

The nice thing about Chicken (and a feature it shares with Gambit) is that it compiles to portable C code, so I wouldn't need an interpreter or runtime library to run the resulting application. This particular application might get distributed in the future, and we didn't want to require a relatively obscure langauge runtime to be distributed with it.

It turns out that adding the feature I wanted wasn't quite trivial, thanks to some lack of clarity in the readline documentation and some in the Chicken manual, but thanks to some very helpful guys on the Chicken email list, I was able to get it figured out. I highly recommend Chicken if you've got requirements similar to mine!

# Posted by: Levi at 1:11 PM on Friday, January 20, 2006
   Comments: (0) Categories: Scheme C

MJD on Typing

I've recently had discussions with people on IRC where I've claimed that C is a flawed language. I think that's a pretty reasonable thing to say, and one of the primary flaws as far as I'm concerned is the type system. I don't dismiss the idea of static typing as fundamentally flawed, only the particular implementation in the C language family. I just came across a talk by M-J Dominus that illustrates my point far better than I managed to.

# Posted by: Levi at 11:48 AM on Monday, August 29, 2005
   Comments: (0) Categories: C Language-design

Floating Point Math in C++ Templates

For a truly evil abuse of C++ template metaprogramming, see this implementation of floating point arithmetic in C++ templates. Apparently it is ridiculously slow to compile (even compared to other C++ code, which is slow to compile to begin with), but beats a hand-coded C floating point implementation in execution speed. C++ is truly a complex and bewildering beast, but definitely powerful in a way that C just isn't.

# Posted by: Levi at 4:36 PM on Wednesday, August 3, 2005
   Comments: (0) Categories: C

A Better C/C++

I recently came across the D Programming Language by Digital Mars. It's a C-family language developed by a C compiler developer that attempts to make a C-like language that is easier for both humans and compilers to deal with. It borrows the good parts of both C++ and Java, but retains system programming features like inline assembly and optional manual memory management. It compares rather well with g++ in benchmarks, too.

The Digital Mars compiler is free, including a standalone compiler and a frontend to gcc. When I get some free time, I'm going to check it out as an option for projects in which I currently use C.

# Posted by: Levi at 2:58 PM on Tuesday, July 19, 2005
   Comments: (0) Categories: C
<< 1 >>