#lang scheme
( define-syntax my-when
( syntax-rules ()
[ ( _ pred body ... )
( if pred ( begin body ... ) ( void ) ) ] ) )
( my-when
( = 2 1 )
( display 1 )
( display 2 ) )
( define-syntax my-let
( syntax-rules ()
[ ( my-let ( [ var exp ] ... )
body ... )
( ( lambda ( var ... )
body ... ) exp ... ) ] ) )
( my-let
( [ a 1 ]
[ b 2 ] )
( display a )
( display b ) )
( define-syntax my-let*
( syntax-rules ()
[ ( my-let* ( [ var exp ] ) body ... )
( my-let ( [ var exp ] ) body ... ) ]
[ ( my-let* ( [ var exp ]
[ var1 exp1 ] ... ) body ... )
( my-let ( [ var exp ] )
( my-let* ( [ var1 exp1 ] ... )
body ... ) ) ] ) )
( define-syntax my-for
( syntax-rules ()
[ ( my-for ( var from to ) body ... )
( let loop ( [ var from ] )
( my-when
( < var to )
body ...
( loop ( + var 1 ) ) ) ) ] ) )
( my-for
( i 0 10 )
( display "a" )
( display "b" ) )
;;; 一种比較有意思的使用方法
( define-syntax loop
( syntax-rules ( begin end from to )
[ ( loop <var> from <min> to <max> start body ... end )
( let loop1 ( [ <var> <min> ] )
( cond
[ ( < <var> <max> ) body ... ( loop1 ( + 1 <var> ) ) ]
[ else ( void ) ] ) ) ] ) )
( loop i from 1 to 10
begin
( display 1 )
( newline )
( display 2 )
( newline )
end )