Algol60 : たらいまわし関数

見つけた。
http://www.csse.monash.edu.au/~lloyd/tildeProgLang/Algol60/
DLしてすぐにサンプルも動いたので、これであれをやってみる。

      • -

やってみました。

'begin'
  'integer' r;

  'integer' 'procedure' tarai(x,y,z);
    'value' x,y,z;
    'integer' x,y,z;
    'begin'
       'if' x <= y 'then'
         tarai := y
       'else'
         tarai := tarai(tarai((x-1), y, z)
                       ,tarai((y-1), z, x)
                       ,tarai((z-1), x, y));
    'end';
  
  r := tarai(10,5,0);
  outstring(1, "tarai(10,5,0) = ");
  outinteger(1, r);
'end'

結果。

$ time a60-v0.20a.exe tarai.a60 
tarai(10,5,0) =  10 
real    0m17.986s
user    0m0.020s
sys     0m0.040s

速くない。なぜかな? やはり、メモ化のせい?

      • -

k.inabaさんより、'value'の指定があると値渡しになってしまうのではという指摘が。
それではということで、コメントアウトして実行してみました。

'begin'
  'integer' r;

  'integer' 'procedure' tarai(x,y,z);
    'COMMENT' 'value' x,y,z;
    'integer' x,y,z;
    'begin'
       'if' x <= y 'then'
         tarai := y
       'else'
         tarai := tarai(tarai((x-1), y, z)
                       ,tarai((y-1), z, x)
                       ,tarai((z-1), x, y));
    'end';
  
  r := tarai(10,5,0);
  outstring(1, "tarai(10,5,0) = ");
  outinteger(1, r);
'end'

結果

$ time a60-v0.20a.exe tarai.a60 
tarai(10,5,0) =  10 
real    0m0.112s
user    0m0.030s
sys     0m0.030s

お、速い!
しかし、数字を大きくするとさすがにだめ。

$ time a60-v0.20a.exe tarai.a60 
tarai(100,50,0) =  100 
real    0m7.921s
user    0m0.030s
sys     0m0.020s

それでは、ということで、xとyだけ値渡しに変更してみる。

  'integer' 'procedure' tarai(x,y,z);
    'value' x,y;
    'integer' x,y,z;

結果はこちら。

$ time a60-v0.20a.exe tarai.a60 
tarai(100,50,0) =  100 
real    0m0.687s
user    0m0.030s
sys     0m0.030s