

A self-infecting datatype in Lisp - gnosis
http://okmij.org/ftp/Scheme/circular-list-infections.txt

======
draven
I'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/...](http://hackage.haskell.org/packages/archive/base/latest/doc/html/src/GHC-
List.html#cycle)

~~~
eridius
This 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>

------
SeanLuke
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)

------
lispm
Nested DEFUNs is not the best idea...

~~~
AUmrysh
As a CL newbie, what would you suggest be used instead? Also, why would you
not use nested defuns?

~~~
draven
labels?
[http://www.lispworks.com/documentation/HyperSpec/Body/s_flet...](http://www.lispworks.com/documentation/HyperSpec/Body/s_flet_.htm)

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...](http://www.lispworks.com/documentation/HyperSpec/Body/m_defun.htm)
"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?

------
guard-of-terra
This looks ubercool until you realise that Clojure does this for a living with
its seq protocol.

Then it's just neat.

