Skip to content

Instantly share code, notes, and snippets.

@sinfu
Created July 30, 2010 20:47
Show Gist options
  • Save sinfu/501291 to your computer and use it in GitHub Desktop.
Save sinfu/501291 to your computer and use it in GitHub Desktop.
Schemeのマクロ好き
#!r6rs
(import (rnrs))
(define (iota-step k n s)
(if (<= s 0) (assertion-violation 'iota "non-positive step" s))
(cond ((> k n) '())
(else (cons k (iota-step (+ k s) n s)))))
(define-syntax iota
(lambda (stx)
(syntax-case stx (< <= by)
[ (_ n ) (syntax (iota-step 0 (- n 1) 1)) ]
[ (_ k < n ) (syntax (iota-step k (- n 1) 1)) ]
[ (_ k < n by s) (syntax (iota-step k (- n 1) s)) ]
[ (_ k <= n ) (syntax (iota-step k n 1)) ]
[ (_ k <= n by s) (syntax (iota-step k n s)) ] )))
(define (println x) (display x) (newline))
(println (iota 5)) ; (0 1 2 3 4)
(println (iota 1 < 5)) ; (1 2 3 4)
(println (iota 1 <= 5)) ; (1 2 3 4 5)
(println (iota 0 < 10 by 2)) ; (0 2 4 6 8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment