News for hackers. Stuff that matters.
Re:A self-infecting datatype in Lisp by draven on Tuesday January 24, 2012 @09:43AM (#3504901)
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/...

Re:A self-infecting datatype in Lisp by eridius on Tuesday January 24, 2012 @03:45PM (#3506671)
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

Re:A self-infecting datatype in Lisp by SeanLuke on Tuesday January 24, 2012 @11:30AM (#3505426)
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)
Re:A self-infecting datatype in Lisp by lispm on Tuesday January 24, 2012 @09:33AM (#3504859)
Nested DEFUNs is not the best idea...
Re:A self-infecting datatype in Lisp by AUmrysh on Tuesday January 24, 2012 @01:02PM (#3505836)
As a CL newbie, what would you suggest be used instead? Also, why would you not use nested defuns?
Re:A self-infecting datatype in Lisp by draven on Tuesday January 24, 2012 @03:17PM (#3506521)
labels? 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_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?

Re:A self-infecting datatype in Lisp by gumbo72 on Tuesday January 24, 2012 @02:16PM (#3506187)
Also interested in this
Re:A self-infecting datatype in Lisp by guard-of-terra on Tuesday January 24, 2012 @08:49AM (#3504677)
This looks ubercool until you realise that Clojure does this for a living with its seq protocol.

Then it's just neat.