#!/usr/bin/guile -s
!#
; I just wanted to implement something with a continuation.
; I'll start with a simple cycle.
; It took me about 15 minutes ;) an awful lot.

; A continuation is a fancy thing, it's like a function
; returning multiple times...

; The simple thing
(define (simple-loop start end action)
    (if (<= start end)
      ((lambda ()
        (action start)
        (simple-loop (+ start 1) end action)))))

; The continuation thing
(define (complex-loop start end action)
  (define *call* 0)
  (if (<=
        (call-with-current-continuation
	  (lambda (cc)
	    (set! *call* cc)
	    start))
        end)
      ((lambda ()
        (action start)
 	(set! start (+ start 1))
	(*call* start)))))

(define action-print (lambda (x) (display x) (newline)))

(simple-loop  1 10 action-print)
(complex-loop 1 10 action-print)
