Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

/prog/ challenge (S(SKK)(SKK))(S(SKK)(SKK))

Name: Anonymous 2011-04-10 9:36

The challenge: Implement generators in the favourite language of yours.
Are banned: Language native generators (i.e. FIOC's yield), delimited continuations (sorry, Schemers, that would be too easy).

The test program will be an infinite generator that computes facts, and a loop that takes the first five of them.

The deadline is 17/04/2011 00:00 /prog/time.
The most complete, small and easy to use implementation wins.

Name: Anonymous 2011-04-12 3:23

Ok, I tried to do it the ``proper'' way, without using macros and shit, but then I realized that I couldn't without having access to the generator procedure's scope or doing some kind of black magic (or implement yield as procedure/macro).
So, think about this way: to maintain the purity of the computation done by the procedure, ``yield'' returns the next ``yield''.
This permits the generator to generally fuck up with the whole program.


(define (gen f)
  (define ((resume-of k y) kk)
    (k (y kk)))
  (define ((continue kk) . x)
    (let/cc k
      (set! resume (resume-of k continue))
      (apply kk x)))
  (define resume
    (resume-of f continue))
  (lambda x
    (call/cc resume)))


(define facts
  (gen (lambda (yield)
         (let loop ((yield yield)
                    (n 1) (r 1))
           (loop (yield r)
                 (+ n 1) (* n r))))))

(for/list ((i (in-range 0 5)))
  (facts))

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List