Paste number 3244: looping using call/cc

Index of paste annotations: 2 | 1

Paste number 3244: looping using call/cc
Pasted by: blackisha
When:5 years, 10 months ago
Share:Tweet this! | http://paste.lisp.org/+2I4
Channel:#scheme
Paste contents:
Raw Source | XML | Display As
(define (cc) (call-with-current-continuation(lambda(x) x)))

(let ((loopstart cc) (i 0))
  (display i)
  (set! i (+ i 1))
  (loopstart)
  )

Annotations for this paste:

Annotation number 2: some more looping with call/cc
Pasted by: velco
When:5 years, 10 months ago
Share:Tweet this! | http://paste.lisp.org/+2I4/2
Paste contents:
Raw Source | Display As
(define (cc) (call-with-current-continuation(lambda(x) x)))
(define i 0)
(let ((loopstart (cc)))
  (display i)
  (newline)
  (set! i (+ i 1))
  (loopstart (cc))
  )

Annotation number 1: Fix
Pasted by: forcer
When:5 years, 10 months ago
Share:Tweet this! | http://paste.lisp.org/+2I4/1
Paste contents:
Raw Source | Display As
;; First of all, you probably wanted to write this:

(let ((loopstart (cc))
      (i 0))
  (display i)
  (set! i (+ i 1))
  (loopstart loopstart))

;; This produces an infinite stream of zeros, before the continuation
;; is in the context where i is still 0. This fixes that problem:

(let ((i 0))
  (let ((loopstart (cc)))
    (display i)
    (set! i (+ i 1))
    (loopstart loopstart)))

;; I assume you are aware of this :-)

(let loop ((i 0))
  (display i)
  (set! i (+ i 1))
  (loop i))

Colorize as:
Show Line Numbers
Index of paste annotations: 2 | 1

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.