Skip to content

Instantly share code, notes, and snippets.

@valvallow
Created March 25, 2010 04:32
cycle
;; cycle
;; Scheme:初心者の質問箱:log00 - http://goo.gl/Clv8
(define (make-cycle ls)
(let ((rest ls))
(lambda ()
(begin0
(car (if (null? rest)
(begin
(set! rest ls)
rest)
rest))
(set! rest (cdr rest))))))
(define c (make-cycle '(1 2 3)))
(dotimes (i 5)
(print (c)))
;; 1
;; 2
;; 3
;; 1
;; 2
;; #t
; again
(define (make-cycle ls)
(let ((l ls))
(lambda ()
(begin0
(car l)
(set! l (if (null? (cdr l))
ls
(cdr l)))))))
(define c (make-cycle '(1 2 3)))
(dotimes (i 5)
(print (c)))
;; 1
;; 2
;; 3
;; 1
;; 2
;; #t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment