Oh geeky livejournal compatriots!

Mar 03, 2006 18:52

I need an simple example of self modifying LISP code from which I can learn and go forth and do evil. Google is being uncharacteristicly unhelpful. Do any of you know of such, or for that matter a repository of good LISP examples period? The future of the world (ok, just this quarter) depends on you!

Leave a comment

Comments 4

xthread March 4 2006, 03:08:33 UTC
This looks neat
But I'm still having trouble tracking down some good eval examples...

Reply


(The comment has been removed)

kamileon March 4 2006, 11:05:16 UTC
*heh* Oddly enough, I have The Little Lisper already in my reading pile, but I'm only a chapter or two in, maybe it had more later. The textbook for the class is Paul Graham's "Ansi Common Lisp", which I'm further on, but it explicitly shies away from talking about self-modifying code (which I thought seemed odd.) (Mind you, it's a fine book in other respects, but it doesn't really have an AI bent, which is more what I was looking for.)

Reply


phreakhead March 6 2006, 21:30:39 UTC
(let) is your friend. Specifically, when you enclose a (defun) inside a (let), whenever that function is called it will have access to the same variables in (let) that it did before. Basically (let) sets global variables that keep their values even when you return from the function.

I guess that doesn't really answer your question. Supposedly (eval) is your main weapon here. Give it a list and it will run that list just as it was a piece of source code. Here's an example from Common Lisp the Language:

(eval (list 'cdr (car '((quote (a . b)) c)))) => b

Obviously this isn't a self-modifying line of code, but it demonstrates what eval does. You could easily store eval's argument (the list above) in a variable and then change parts of it if you need to.

The real question is what do you want to do with self-modifying code? It's not really useful except in very specific circumstances (like a virus changing its code to evade virus detectors, ie "polymorphism". However, no one writes viruses in LISP, and the re-written code is usually ( ... )

Reply

kamileon March 6 2006, 21:49:25 UTC
Short answer: genetic algorithms.
Longer answer: in AI this quarter, for the last tournament, we have the option of either returning a play, or returning a lambda function that acts on the top 5 plays (unless you're in the top 5). It would be nice to be able to tune and tweak that lambda function on the fly as we learn, but to do that, that probably means self modifying code.

And because it's cool. :)

Reply


Leave a comment

Up