self-destruct

Q&A's, tips, howto's
Locked
newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

self-destruct

Post by newdep »

As the oposite of creating macro's from macro's or functions from
funtions I wanted something that deleted itself after execution..

Sound logical indeed but correct me if im wrong but in previous
releases of newlisp you couldn't destroy yourself as being a variable
or function...

Seems that indeed now the destructive-save function 'copy is introduced
you can destroy yourself (finaly!..who doesnt want that ;-)

*edited* seems that for functions the 'copy isnt needed only
the 'catch is enough


there is

Code: Select all

(MAIN)-> (setq self-destruct (copy (delete 'self-destruct)))
true
(MAIN)-> self-destruct
nil
And

Code: Select all

(MAIN)-> (define self-destruct (catch (copy (delete 'self-destruct))))
?
(MAIN)-> self-destruct
nil
And there is

Code: Select all

(MAIN)-> (define (self-destruct) (catch (copy (delete 'self-destruct))))
(lambda () (catch (copy (delete 'self-destruct))))
(MAIN)-> (self-destruct)
true
(MAIN)-> (self-destruct)
ERR: invalid function : (self-destruct)
(MAIN)-> self-destruct
nil

And namespaced

Code: Select all

(MAIN)-> (define (self:destruct) (catch (copy (delete 'self:destruct))))
(lambda () (catch (copy (delete 'self:destruct))))
(MAIN)-> (self:destruct)
true


What surprices me is that 'Catch indeed catches here the error, because if you dont the function will crash newlisp..

The '?' is probably a tiny bug? Or is it indeed a BIG Question to newlisp ;-)
-- (define? (Cornflakes))

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

You still cannot self-destruct functions, you are just lucky :-)

The '?' tells that newLISP is displaying a lisp cell already deallocated. You are not far away from a crash.

But you could write a harakiri function doing the job:

Code: Select all

(define-macro (切腹)
    (let (temp (eval (args)))
      (delete (args 0))
      temp))

(define (foo x) (+ x x))

(切腹 foo 123) => 246

(sym "foo" MAIN nil) => nil; symbol foo does not exist anymore

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

Heheh.. Well actualy "being Lucky at self-destruct" is partly a good function
as you can only do it once, I tried indeed the harakiri but I did not want
to involve a 2nd party...

PS the above is about a newlisp function..just to make that clear ;-)

PPS Harakiri is an individual action where the Sepuku i think was done
by a 2nd 'close releative' party.. Anyway options enough...
Last edited by newdep on Tue Apr 07, 2009 6:21 pm, edited 1 time in total.
-- (define? (Cornflakes))

DrDave
Posts: 126
Joined: Wed May 21, 2008 2:47 pm

Post by DrDave »

newdep wrote:Heheh.. Well actualy "being Lucky at self-destruct" is partly a good function
as you can only do it ones, I tried indeed the harakiri but I did not want
to involve a 2nd party...

PS the above is about a newlisp function..just to make that clear ;-)

PPS Harakiri is an individual action where the Sepuku i think was done
by a 2nd 'close releative' party.. Anyway options enough...
I visited a friend in Japan a few years ago and some discussion about harakiri developed. If I recall correctly, sepuku and harakiri are essentially synonyms in contemporary Japanese. But I think sepuku is reserved for formal language like in literature, and harakiri is spoken on the street. In any case, they were to be used only by Samarai warriors. (But really, would anyone else but a Samuarai want to slice themselves in the abdomen on purpose?)

By the way, you WANT to be lucky (successful) when attempting harakiri, or you are in even greater shame than whatever condition brought you to the point of harakiri in the first place.

EDIT--OK, I did some checking on this. Sepuku USUALLY involved a "second", a trusted swordsman that, upon a previously arranged moment after the abdominal slashing, sliced off the victim's head, thereby providing a quick and honorable death. The most desirable condition was to leave the head attached by a strip of skin, so that it did not roll off and make a mess on someone nearby.
...it is better to first strive for clarity and correctness and to make programs efficient only if really needed.
"Getting Started with Erlang" version 5.6.2

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

Thanks for clearing that up ;-) Because I was already wondering what the
name then should be.. And Why in heavens name I remebered that word ;-)
-- (define? (Cornflakes))

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

Yes indeed.. 'copy has nothing to do with this..
Whatever encapsulates the 'delete will return 'true.
without it it returns the '?' or a crash...

So this wont work all the time, its probably related to the internals
of newlisp that this now works.. stack etc...


But... Im still fasinated by a 'self-destruct function..
-- (define? (Cornflakes))

Locked