#!r6rs ;;;; A macro that ressembles the Python for syntax, basically a ;;;; macro plaything to find out how to write semi-useful macros. ;;;; This file should be able to run in PLT Scheme, Ikarus Scheme ;;;; and Ypsilon without changes. ;;;; ;;;; 2010 by Marek Kubica ;;; get some r6rs primitives (import (rnrs base) (rnrs control) (rnrs io simple)) (define-syntax py-for ;; 'in' is a keyword (syntax-rules (in) ;; first the case with a list of identifiers (general case) ((_ (?bindings ...) in ?collection body ...) ;; construct the binding that maps list items to their identifiers (let ((code (lambda (?bindings ...) body ...))) ;; loop for every item in the collection and apply the lambda (let loop ((collect ?collection)) (unless (null? collect) (apply code (car collect)) (loop (cdr collect)))))) ;; the case with only a single binding transforms into the general case ((_ ?binding in ?collection body ...) (py-for (?binding) in (map list ?collection) body ...)))) ;;; example code to verify the results (py-for x in '(1 2 3) (display x)) (py-for (x y) in '((a b) (A B) (1 2)) (display x) (display y))