Newsgroups: comp.lang.lisp
Path: cantaloupe.srv.cs.cmu.edu!das-news2.harvard.edu!oitnews.harvard.edu!purdue!haven.umd.edu!cs.umd.edu!news.abs.net!explorer.csc.com!uunet!in3.uu.net!205.252.116.190!feed1.news.erols.com!howland.erols.net!netcom.com!vrotney
From: vrotney@netcom.com (William Paul Vrotney)
Subject: Re: Self-Modifying Lisp Programs?
In-Reply-To: njames@uoguelph.ca's message of 9 Dec 1996 02:04:18 GMT
Message-ID: <vrotneyE272wz.AG8@netcom.com>
Organization: NETCOM On-line Communication Services (408 261-4700 guest)
References: <58fs32$1ea@ccshst05.cs.uoguelph.ca>
Date: Tue, 10 Dec 1996 11:07:47 GMT
Lines: 47
Sender: vrotney@netcom6.netcom.com


In article <58fs32$1ea@ccshst05.cs.uoguelph.ca> njames@uoguelph.ca (Nick D James) writes:

> 
> Since Lisp data structures and Lisp code have the same syntactic 
> structure, and since the 'eval' command allows data to be treated as 
> code, and the 'quote' command allows code to be treated as data, Lisp has 
> the potential to support self-rewriting programs.  I have heard people 
> refer to these, but I've never actually seen one.  Could anyone give me a 
> reference?  Has anyone here ever written a self-modifying Lisp program?
> I think this is really exciting, and I'd be most grateful if anyone could 
> tell me more.
> 

There was a fad several years ago called "core wars".  I don't know if there
are still people doing this or not.  The idea was to write an assembly
language program that modified itself and other parts of memory.  For
example it could write itself to another part of memory and then execute
itself there.  A game was made out of this by having two such programs start
at the same time in effect.  The program that was clever enough to survive
the other programs memory writes would not break and hence was declared the
victor.

I suspect there is some science to self modifying programs, but I don't know
of any.  Maybe somebody else does and could speak up.

You are right, it is easy to write self modifying Lisp programs.  For
example the following should work

 (setq /a
       '(lambda (x)
          (print (length x))
          (setq /a (list* (first x) (second x) (third x) (third x) (cdddr x)))
          (funcall x /a)))

 (funcall /a /a)

Its output is probably as exciting as watching paint dry.  One challenge
would be to write a more interesting self modifying program.

If this coincidently happens to be an answer for some students' exercise, I
recommend modifying the above program a bit so that all students don't have
the same answer.

-- 

William P. Vrotney - vrotney@netcom.com
