self-destruct

Started by newdep, April 07, 2009, 03:51:00 AM

Previous topic - Next topic

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

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


And



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


And there is

(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

(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

#1
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:


(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

#2
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...
-- (define? (Cornflakes))

DrDave

#3
Quote from: "newdep"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

#4
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

#5
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))