Created
April 10, 2012 05:44
-
-
Save mh61503891/2348523 to your computer and use it in GitHub Desktop.
ネストしたイテレート処理の比較(Java、Ruby、Prolog)
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
※0オリジンか1オリジンという意味では等価なコードではない。 | |
// written in Java | |
for(int k = 0, k < K, k++) | |
for(int j = 0, j < J, j++) | |
for(int i = 0, i < I, i++) | |
execute(i, j, k); | |
# written in Ruby | |
K.times{ |k| | |
J.times{ |j| | |
I.times{ |i| | |
execute(i, j, k) | |
} | |
} | |
} | |
% written in Prolog | |
sub1(I, J, 0). | |
sub1(I, J, K) :- | |
sub2(I, J, K), | |
NextK is K - 1, | |
sub1(I, J, NextK). | |
sub2(I, 0, K). | |
sub2(I, J, K) :- | |
sub3(I, J, K), | |
NextJ is J - 1, | |
sub2(I, NextJ, K). | |
sub3(0, J, K). | |
sub3(I, J, K) :- | |
execute(I, J, K), | |
NextI is I - 1, | |
sub3(NextI, J, K). | |
# written in Prolog | |
forall(between(0, MaxK, K), | |
forall(between(0, MaxJ, J), | |
forall(between(0, MaxI, I), | |
(execute(I, J, K))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment