Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search

[REBOL] Re: A Rebol Challenge. The Monty Hall Puzzle IN 0 BY TES

From: sunandadh:aol at: 17-Dec-2001 19:58

Hi Reichart,
> This exact same problem in other languages would be just: > Matlab: sum(rand(100,1)>1/3) > C: for(int i=0,j=0;i<100;i++)j+=rand(3)>1;cout<<j; > In Perl: $i=0;$j=0;for(;$i<100;$i++){$j+=rand(3)>1;}print $j > Rebol: w: 100 print [loop w [w: w - first random [0 0 1]]] > > Does not look good for Rebol.
We can use the same algorithm as you have for Perl and C, and get something like: s: 0 n: 100 loop n[if 1 < Random 3 [s: s + 1]]s Which is 47 bytes -- exactly the same as your C (actually C++) example And shorter than your Perl, though longer than Joel's improvement. (You ask that we count the Rebol [] as an extra 8 bytes overhead. Fair enough, but shouldn't your C++ have a standard header include?) Unlike Perl and C, Rebol needs an explicit type cast to go from true/false to an integer, so to follow exactly the Perl/C++ examples we'd have to write something like: s: 0 n: 100 loop n[s: s + to-integer (1 < Random 3)] or s: 0 n: 100 loop n[s: s + pick[0 1](2 < Random 3)] but they are longer. Rebol "loses out" on sheer shortness here because it needs spaces between operators, and its RAND function is spelt RANDOM. Some people would see these as sufficient compensation for coming second in such a race. An, of course, bear in mind that the entire Rebol environment needed to run these programs is under 252K (Rebol/Core, Windows 3.1). If I am allowed to have a slightly larger environment (though still way smaller than C or Perl -- I don't know about Matlab), then I'd define: L: :Loop R: :Random And roll my code home in 39 bytes: s: 0 n: 100 L n[if 1 < R 3 [s: s + 1]]s Sunanda.