Thursday, February 26, 2009

I forgot I had a blog

It's plausible that I forgot it intentionally, and I did it so well that I forgot that I forgot it.

I still don't get to do anything with Lisp at work, but I'm studying Haskell in my spare time. My ideal language would have a Lisp syntax, a strong macro system, and a Hindley-Milner variant for a type system. I've been doing statically typed stuff so long that I'm really comfortable with static typing and I know enough about machine code to lust after type erasure.

I'm keeping an eye on BitC because it could be the real deal.

Thursday, April 26, 2007

I've learnt some CFFI while trying to play with SDL (bottom line being, Macs are weird, I shoulda known better, *shakes fist*) and it's real smooth stuff. It's still not second nature, but it's really nice to read through the tutorial for a library, encounter a function or a type you haven't used yet, enter a simple declaration or two in my running SBCL image, and start playing around with the function to see how it works.

Tonight I was trying to learn to use SDL_audio (which is nice, because it forces me to learn how to do callbacks in CFFI) and I made a simple white noise generator. So it turns out it's not a good idea to cons in a callback that is called 44100 times a second. :D I managed to get the error message "Whoa! allocation trap and we weren't in lisp!" and had an uninterruptible stream of white noise coming out of my speakers while frantically trying to bring up a terminal to kill sbcl, my hard drive thrashing as it swapped out virtual memory it never knew it had.

Who says I don't ever do anything exciting?

Tuesday, January 16, 2007

Lexical closures in Common Lisp vs. Lua

I don't get to do too much with dynamic languages at work, so I was really happy to get a chance to write some Lua today. I wanted to make a function that would create a timer that would execute a callback when it was finished, and long story short, I started experimenting with closures to see exactly how they worked in Lua. I came across something that I didn't entirely expect. First, an example in CL:
(mapc (lambda (x) (funcall x))
(loop for i from 1 to 3 collect
(let ((localvar i))
(lambda ()
(format t "loop:~a, local:~a~%" i localvar)))))
This outputs:
loop:4, local:1
loop:4, local:2
loop:4, local:3
which makes sense to me, now that I've been working in Common Lisp. Inside the inner lambda, i refers to the loop variable, which is at 4 at the end of the loop. During each iteration of the loop, a new binding is created for localvar, and then the inner lambda closes over it. That's why we have different values for local but the same value for loop.

However, the closest equivalent I can manage in Lua is this (apologies for the temporary):
collection={}
for i=1,3 do
collection [i] = function ()
local localvar = i
print("loop:" .. i .. ", local:" .. localvar .. "\n")
end
end
for k,v in pairs (collection) do
v()
end
And this outputs:
loop:1, local:1
loop:2, local:2
loop:3, local:3
So, it looks like Lua actually closes over the value, whereas Lisp closes over the binding. I'm trying to think of a way to force Lua to close over the binding by boxing the value or something, but I'm coming up dry.

Wednesday, November 15, 2006

All I remember is that I was walking around the labyrinthine grounds at work with my team and all of a sudden a dog popped out of one of the meeting rooms. Discussion immediately ensued as to what to do, and nothing was happening, so I picked up a leash that I noticed hanging from its collar. Immediately, people starting saying "Oh, it has a leash! Well, that changes everything, now we should ..." I started walking the dog toward the information desk, and eventually people noticed I was taking care of it without discussion, and just fell silent.

Later on I noticed that the leash was actually a brace leash (a kind of leash used to walk two dogs close to each other) and that the other animal on it was a tabby cat. It was a very attentive, perky, doggishly-acting tabby cat, but it was still a cat. We went walking about the city and other stuff started happening that I don't remember, but at one point, I noticed that the dog and cat were no longer there, and I became frantic and woke up. Upon waking and being foggy, the realization set in that since the dog and cat were a dream and didn't actually exist, it wasn't that I had lost them and they'd gone off on their own, it was that because I didn't sustain focus, they actually failed to exist. This was devastating for about fifteen minutes.

Huh.

Tuesday, October 31, 2006

Hello, new blog!