Skip to content

Instantly share code, notes, and snippets.

@cametan001
Created July 26, 2010 00:40
Show Gist options
  • Select an option

  • Save cametan001/490035 to your computer and use it in GitHub Desktop.

Select an option

Save cametan001/490035 to your computer and use it in GitHub Desktop.
;;; ある範囲の数に対しての繰り返しオペレータfor
(define-syntax for
(syntax-rules ()
((_ index start end body ...)
(do ((index start (+ index 1)))
((> index end) #f)
body ...))))
;; 実行例:
;; > (for i 1 10
;; (display i)
;; (display " "))
;; 1 2 3 4 5 6 7 8 9 10 #f
;; >
;;; リストまたは文字列の要素を通して繰り返すためのeach
(define-syntax each
(syntax-rules ()
((_ var seq body ...)
(begin
(for-each (lambda (var)
body ...) (cond ((list? seq)
seq)
((string? seq)
(string->list seq))
(else
(error "invalid data type : " seq))))
#f))))
;; 実行例:
;; > (each x '(a b c d e)
;; (display x)
;; (display " "))
;; a b c d e #f
;;; ある条件が真である間に繰り返し続けるためのwhile
(define-syntax while
(syntax-rules ()
((_ (pred? arg ...) body ...)
(do ()
((not (pred? arg ...)) #f)
body ...))))
;; 実行例:
;; > (let ((x 10))
;; (while (> x 5)
;; (set! x (- x 1))
;; (display x)))
;; 98765#f
;; >
;;; 何かをn回行なう単純なrepeat
(define-syntax repeat
(syntax-rules ()
((_ num body ...)
(do ((var num (- var 1)))
((zero? var) #f)
body ...))))
;; 実行例:
;; > (repeat 5 (display "la "))
;; la la la la la #f
;; >
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment