A self-infecting datatype in Lisp
okmij.org
okmij.org
The code for the cycle Haskell function is really simple: http://hackage.haskell.org/packages/archive/base/latest/doc/...
;; 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)My guess: the nested defun is not only defined in the enclosing defun scope as it may seem, but in the whole current package.
http://www.lispworks.com/documentation/HyperSpec/Body/m_defu... "Defines a new function named function-name in the global environment"
Is it only a bad idea because it's bad style or is there more to it?
Then it's just neat.