3 ms·
A self-infecting datatype in Lisp
- guard-of-terra 15y agoThis looks ubercool until you realise that Clojure does this for a living with its seq protocol. Then it's just neat.
- lispm 15y agoNested DEFUNs is not the best idea...
- AUmrysh 15y agoAs a CL newbie, what would you suggest be used instead? Also, why would you not use nested defuns?
- gumbo72 15y agoAlso interested in this
- draven 15y agolabels? http://www.lispworks.com/documentation/HyperSpec/Body/s_flet_.htm http://www.lispworks.com/documentation/HyperSpec/Body/s_flet... 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_defun.htm 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?
- draven 15y agoI'm learning Haskell ATM and it looks similar to lazy lists in WHNF, as at each step you get an element and a "thunk" that can be used to compute the next step. The code for the cycle Haskell function is really simple: http://hackage.haskell.org/packages/archive/base/latest/doc/html/src/GHC-List.html#cycle http://hackage.haskell.org/packages/archive/base/latest/doc/...
- eridius 15y agoThis is similar to one of my favorite Haskell "tricks": Tying the Knot. It's kind of a mindfuck the first time you do it, but it also feels really really cool. http://www.haskell.org/haskellwiki/Tying_the_Knot http://www.haskell.org/haskellwiki/Tying_the_Knot
- SeanLuke 15y agoSince 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)