2 ms·
Since he mentioned self-infecting Lisp functions (which seems to me to be unrelated to his main topic but whatever), I should mention that I put together a Lisp
by SeanLuke 15y ago
Since he mentioned self-infecting Lisp functions (which seems to me to be unrelated to his main topic but whatever), I should mention that I put together a Lisp self-infecting "virus" (so to speak) a long time ago. My original version -- which was beautiful and I have conveniently forgotten -- used a quine to deliver the payload. But here's a version I whipped up for HN a few weeks back which gives you the idea.
;; run this on SBCL
(sb-ext:unlock-package :common-lisp)
(let (syms) (do-symbols (s :common-lisp) (if (fboundp s) (push s syms)))
(labels ((worm (sym)
(let ((next (elt syms (random (length syms))))
(old (symbol-function sym)))
(format t "~%Worm attacks ~a, next target is ~a!~%" sym next)
(setf (symbol-function sym)
(lambda (&rest args)
(when next (worm next) (setf next nil)) (apply old args))))))
(worm '+)))
;; now try adding some numbers (and so on)