いてれぇと

元ネタ(by dm)。
$ cat test.rb
a = (1 .. 10).to_a;
a.each { |x|
    p x;
    a.delete(x);
}
$ ruby test.rb
1
3
5
7
9
適当にリファレンスを読みつつ、他の言語でも書いてみた。
$ cat test.pl
@a = (1 .. 10);
for $x (@a) {
    print "$x\n";
    for $i (0 .. $#a) {
        splice(@a, $i, 1) if $a[$i] == $x;
    }
}
$ perl test.pl
1
3
5
7
9
$ cat test.php
<?php
$a = range(1, 10);
foreach ($a as $x) {
    print "$x\n";
    for ($i = 0; $i < count($a); $i++)
        if ($a[$i] == $x) array_splice($a, $i, 1);
}
?>
$ php test.php
1
2
3
4
5
6
7
8
9
10
$ cat test2.php
<?php
$a = range(1, 10);
while (list(, $x) = each($a)) {
    print "$x\n";
    for ($i = 0; $i < count($a); $i++)
        if ($a[$i] == $x + 1) array_splice($a, $i, 1);
}
?>
$ php test2.php
1
1
3
1
3
5
1
3
5
7
1
3
5
7
9
1
3
5
7
9
$ cat test.ml 
let t = Hashtbl.create 51 in 
  for x = 1 to 10 do Hashtbl.add t x () done;
  Hashtbl.iter (fun x () -> 
    Printf.printf "%d\n" x;
    Hashtbl.remove t x) t
$ ocaml test.ml
1
2
3
4
5
6
7
8
9
10
$ cat test2.ml
let t = Hashtbl.create 51 in 
  for x = 1 to 10 do Hashtbl.add t x () done;
  Hashtbl.iter (fun x () -> 
    Printf.printf "%d\n" x;
    Hashtbl.remove t (x + 1)) t
$ ocaml test2.ml
1
3
5
7
9
$ cat test.cpp
#include <algorithm>
#include <vector>
#include <iostream>
std::vector<int> v;
void f(int x) {
    std::cout << x << std::endl;
    std::remove(v.begin(), v.end(), x);
}
int main() {
    for (int i = 1; i <= 10; i++)
        v.push_back(i);
    std::for_each(v.begin(), v.end(), f);
}
$ g++ test.cpp && ./a.out
1
3
5
7
9
10
10
10
10
10
逆。
$ cat test.rb
a = (1 .. 10).to_a;
a.each { |x|
    p x;
    a.push(x + 10);
}
$ ruby test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
... 以下略 ...
$ cat test.pl
@a = (1 .. 10);
for $x (@a) {
    print "$x\n";
    push(@a, $x + 10);
}
$ perl test.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
... 以下略 ...
$ cat test.php
<?php
$a = range(1, 10);
foreach ($a as $x) {
    print "$x\n";
    array_push($a, $x + 10);
}
?>
$ php test.php
1
2
3
4
5
6
7
8
9
10
$ cat test2.php
<?php
$a = range(1, 10);
while (list(, $x) = each($a)) {
    print "$x\n";
    array_push($a, $x + 10);
}
?>
$ php test2.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
... 以下略 ...
$ cat test.ml
let t = Hashtbl.create 51 in 
  for x = 1 to 10 do Hashtbl.add t x () done;
  Hashtbl.iter (fun x () -> 
    Printf.printf "%d\n" x;
    Hashtbl.add t (x + 10) ()) t
$ ocaml test.ml
1
2
3
4
5
... 中略 ...
46
47
48
49
50
$ cat test2.ml
let t = Hashtbl.create 51 in 
  for x = 1 to 10 do Hashtbl.add t x () done;
  Hashtbl.iter (fun x () -> 
    Printf.printf "%d\n" x;
    Hashtbl.add t (-x) ()) t
$ ocaml test2.ml
1
2
3
4
5
6
-6
7
-5
8
-4
9
-3
10
-2
-1
$ cat test.cpp
#include <algorithm>
#include <vector>
#include <iostream>
std::vector<int> v;
void f(int x) {
    std::cout << x << std::endl;
    v.push_back(x + 10);
}
int main() {
    for (int i = 1; i <= 10; i++)
        v.push_back(i);
    std::for_each(v.begin(), v.end(), f);
}
$ g++ test.cpp && ./a.out 
1
2
3
4
5
6
7
8
9
10
寸評:PerlRuby は仲良し。PHP 天の邪鬼。OCaml そう来るか。C++ 素直でよろしいです。