Monday, November 27, 2006


In C, I'm used to swapping values with a temporary:

int x = 1;
int y = 2;

int tmp = x;
x = y;
y = tmp;

In Lisp, you can do the same thing using psetf:

(let ((x 1) (y 2))
  (psetf x y y x))


I'm doing more math-wise in Lisp as I am playing around with a little bit of rendering. psetf is an example of one of those small corner-case functions that greatly simplify coding in Lisp. Going through the kind of "and here are all the NUMBER functions..." reference doesn't penetrate my thick skull. Necessity is the mother of memorization (or something like that). I am moving forward on the working assumption that no matter what it is I am coding, there is probably a simpler way to do it. This isn't necessarily always the case. There are a few areas where Lisp seems overly verbose (multiple return values in a let comes to mind); however, it is a pretty good chance that if it looks ugly, Lisp has a better way of doing it. The picture above is my anti-aliased line rendering courtesy of Xach's Salza package and example png.lisp code. I'm rolling my own as a hobby/exercise more than anything else. I've got some particular image rendering goals, so this is probably not going to be that generic. I don't care about rotating bitmaps, color correction, or any of that image manipulation stuff. What I do want to do is render nice anti-aliased curves, text and gradients.

And even when I've found a simpler way, I am probably still missing something. Thas on #lisp pointed out that the simple swap above could be done with (rotatef x y) much more concisely. In my case, I am actually performing several swaps at once based on the slope of the line:

(when (minusp dy)
    x1 x2 x2 x1
    y1 y2 y2 y1
    dx (- x1 x2)
    dy (- y1 y2)))

But, for the simple swap, rotatef is your man.



Post a Comment

<< Home