for をより for らしく書き直していったつもりでしたが、do マクロに近づいて行きました。do マクロと聞くと do ~ while とか do ~ until などを連想しますが、for に近いですね。というか、for じゃないですか。
これは、for というより nfor といった感じでしょうか。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define-syntax for | |
(syntax-rules () | |
((_ (var from to) body ...) | |
(let loop ((var from)) | |
(when (< var to) | |
body ... | |
(loop (+ var 1))))))) |
step を追加。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define-syntax for | |
(syntax-rules () | |
((_ (var from to step) body ...) | |
(let loop ((var from)) | |
(when (< var to) | |
body ... | |
(loop (+ var step))))) | |
((_ (var from to) body ...) | |
(for (var from to 1) body ...)))) |
練習がてら do マクロで書き直し。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define-syntax for | |
(syntax-rules () | |
((_ (var from to step) body ...) | |
(do ((var from (+ var step))) | |
((not (< var to))) | |
body ...)))) |
数値に限らないように書き直し。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define-syntax for | |
(syntax-rules () | |
((_ (var init pred update) body ...) | |
(let loop ((var init)) | |
(when pred | |
body ... | |
(loop update)))) | |
((_ (var pred update) body ...) | |
(for (var #f pred update) body ...)))) |
再度 do マクロ。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define-syntax for | |
(syntax-rules () | |
((_ (var init pred update) body ...) | |
(do ((var init)) | |
((not pred)) | |
body ... | |
update)) | |
((_ (var pred update) body ...) | |
(for (var #f pred update) body ...)))) |
さらに、for らしく。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define-syntax for | |
(syntax-rules () | |
((_ (((var init) ...) | |
(pred ...)(update ...)) body ...) | |
(do ((var init) ...) | |
((not (and pred ...))) | |
body ... | |
update ...)))) |
この辺で、劣化 do マクロを書いている様な気がしてきたので終了。
for って自由度高いですよね
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for (var today = new Date(), | |
y = today.getFullYear(), | |
m = today.getMonth(), | |
bom = new Date(y, m, 1), | |
eom = new Date(y, m + 1, 0), | |
day = 1; | |
day <= eom.getDate(); | |
day++){ | |
alert (day); | |
}; | |
for (var i = 0; i < 10; i % 2 == 0 ? i % 4 == 0 ? i = i + 2 : i = i + 1 : i = i + 1) { | |
alert (i); | |
} | |
var i = 0; | |
for (;;){ | |
if (10 < i){ | |
break; | |
} | |
alert (i++); | |
} | |
for (var b1 = false, b2 = false, i = 0; b1 !== b2, i !== 50;){ | |
if (i == 10){ | |
b1 = true; | |
} | |
if (i == 20){ | |
b2 = true; | |
} | |
alert(i++); | |
} |
0 件のコメント:
コメントを投稿