Skip to content

Instantly share code, notes, and snippets.

@valvallow
Created May 25, 2010 13:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save valvallow/413146 to your computer and use it in GitHub Desktop.
Save valvallow/413146 to your computer and use it in GitHub Desktop.
dotimes, syntax, scheme
;; dotimes
(define-syntax dotimes
(syntax-rules ()
((_ n body ...)
(do ((i n (- i 1)))
((not (< 0 i)))
body ...))))
(dotimes 5
(display "hello")
(print "world"))
;; helloworld
;; helloworld
;; helloworld
;; helloworld
;; helloworld
;; #t
(define-syntax dotimes
(syntax-rules ()
((_ n body ...)
(let loop ((i n))
(when (< 0 i)
body ...
(loop (- i 1)))))))
(dotimes 5
(print "H")
(print "l")
(print "l")
(print "o"))
;; H
;; l
;; l
;; o
;; H
;; l
;; l
;; o
;; H
;; l
;; l
;; o
;; H
;; l
;; l
;; o
;; H
;; l
;; l
;; o
;; #<undef>
;; dotimes
(define-syntax dotimes
(syntax-rules ()
((_ n body ...)
(let loop ((i n))
(when (< 0 i)
body ...
(loop (- i 1)))))))
(dotimes 5 (print 1))
(define i 10)
(dotimes 5 (print (set! i (+ i 1))))
;; 11
;; 12
;; 13
;; 14
;; 15
(macroexpand-1 '(dotimes 5 (print (set! i (+ i 1)))))
;; (#<identifier user#let> #0=#<identifier user#loop>
;; ((#1=#<identifier user#i> 5))
;; (#<identifier user#when>
;; (#<identifier user#<> 0 #1#)
;; (print (set! i (+ i 1)))
;; (#0# (#<identifier user#-> #1# 1))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment