Path: utzoo!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!samsung!munnari.oz.au!yoyo.aarnet.edu.au!sirius.ucs.adelaide.edu.au!levels!majgr From: Jenny.Rowland@levels.sait.edu.au Newsgroups: comp.lang.lisp Subject: Re: Self-modifying function Message-ID: <15983.27dfd64c@levels.sait.edu.au> Date: 14 Mar 91 09:30:12 GMT References: <11609@jpl-devvax.JPL.NASA.GOV> Organization: University of South Australia Lines: 43 In article <11609@jpl-devvax.JPL.NASA.GOV>, charest@ai-cyclops.jpl.nasa.gov writes: > I want to define a function that performs a certain computation on its first > invocation and performs a different computation on all subsequent invocations. > Consider the trivial (but representative) example below: > > (defun mutate (list) > (let ((len (length list))) > (prog1 > list > (compile 'mutate > `(lambda () > ,len))))) > > 1> (mutate '(a b c)) > => (a b c) > 2> (mutate '(a b c)) > => Error: too many arguments to function mutate. > 3> (mutate) > => 3 > > My question is, Will this function behave as expected (i.e., as described > above) in any Common LISP? More specifically, is it safe to modify the contents > of a symbols' function cell even as the contents are being executed? I can find > nothing in CLtL2 that addresses this. I welcome all opinions, learned or > otherwise. > > -Len Charest > charest@ai-cyclops.jpl.nasa.gov You could try something like: (defun mutate (list) (let ((len (length list))) (setf (symbol-function 'mutate) #'(lambda () len)) list)) I am not one for altering function definitions on the fly - but if one must, this version seems 'nicer' than the other. I would welcome any comments on it however. -Jenny Rowland jenny.rowland@sait.edu.au