2010-10-26 21 views
15

您可以編寫的最複雜/最強大的代碼是什麼,它在最大數量的語言中是有效的?您可以編寫的最複雜/最強大的代碼是什麼,它在最大數量的語言中是有效的?

因此,例如,以下代碼在PHP和JavaScript中均有效。

function foo($bar) 
{ 
$var = 0; 
for($i=0;$i<100;$i++) 
{ 
$var += ($bar + 2)/(1+$i); 
} 
return $var; 
} 

爲什麼這是一個有用的挑戰?我認爲它是一種思考相似語言如何可行的有用方法,並且知道一個人的語法可以轉化爲學習他人的語法。

+1

@comment我,如果這被關閉,你有我重新投票(由於我退出投票,因此無法立即投票) – 2010-10-26 20:56:28

+0

你想讓它成爲社區wiki嗎? – Gumbo 2010-10-26 20:58:31

+1

rosettacode.org可能是找出答案的好資源。 – 2010-11-03 21:53:38

回答

18

這被稱爲Polyglot,它可以變得非常複雜。這裏有一個以八種語言:http://ideology.com.au/polyglot/

Cobol語言,帕斯卡爾,FORTRAN,C,的PostScript,慶典,x86機器語言和Perl:

                  (*O/*_/ 
Cu #%*)pop mark/CuG 4 def/# 2 def%%%%@@P[TX---P\P_SXPY!Ex(mx2ex("SX!Ex4P)Ex= 
CuG #%*                 *+Ex= 
CuG #%*------------------------------------------------------------------*+Ex= 
CuG #%* POLYGLOT - a program in eight languages  15 February 1991 *+Ex= 
CuG #%* 10th Anniversary Edition      1 December 2001 *+Ex= 
CuG #%*                 *+Ex= 
CuG #%* Written by Kevin Bungard, Peter Lisle, and Chris Tham   *+Ex= 
CuG #%*                 *+Ex= 
CuG #%* Polyglot suports the following languages:      *+Ex= 
CuG #%*  1. COBOL (ANSI)            *+Ex= 
CuG #%*  2. Pascal (ISO)            *+Ex= 
CuG #%*  3. Fortran (ANSI, f77)          *+Ex= 
CuG #%*  4. C (ANSI-ish)            *+Ex= 
CuG #%*  5. PostScript            *+Ex= 
CuG #%*  6. Linux/Unix shell script (bash, sh, csh)     *+Ex= 
CuG #%*  7. x86 machine language (MS-DOS, Win32, Linux)    *+Ex= 
CuG #%*  8. Perl (version 5)           *+Ex= 
CuG #%*                 *+Ex= 
CuG #%* Usage:               *+Ex= 
CuG #%*  1. Rename this file to polyglot.[cob|pas|f77|c|ps|sh|com|pl] *+Ex= 
CuG #%*                 *+Ex= 
CuG #%*  2. Compile and run with your favorite compiler and operating *+Ex= 
CuG #%*  system.             *+Ex= 
CuG #%*                 *+Ex= 
CuG #%* Notes:               *+Ex= 
CuG #%*  1. We have attempted to use only standard language features. *+Ex= 
CuG #%*                 *+Ex= 
CuG #%*  2. Without the -traditional flag gcc will issue a warning. *+Ex= 
CuG #%*                 *+Ex= 
CuG #%*  3. When transferring from Unix to DOS make sure that a LF *+Ex= 
CuG #%*  is correctly translated into a CR/LF.      *+Ex= 
CuG #%*                 *+Ex= 
CuG #%*  4. Keep the blank lines at the start of the program. They *+Ex= 
CuG #%*  are important.           *+Ex= 
CuG #%*                 *+Ex= 
CuG #%*  5. This text is a comment block in all eight languages.  *+Ex= 
CuG #%*                 *+Ex= 
CuG #%* Please mail any comments, corrections or additions to   *+Ex= 
CuG #%* [email protected]          *+Ex= 
CuG #%*                 *+Ex= 
CuG #%*------------------------------------------------------------------*QuZ= 
CuG #%*                 *+Ex= 
CuG #%*!Mx)ExQX5ZZ5SSP5n*5X!)Ex+ExPQXH,B+ExP[-9A-9B(g?(gA'UTTER_XYZZXX!X *+ 
CuG #(*                 *(
C # */);                /*(
C # *) program  polyglot (output);        (*+ 
C #  identification division. 
C #  program-id. polyglot. 
C # 
C #  data   division. 
C #  procedure  division. 
C # 
C # *))cleartomark /Bookman-Demi findfont 36 scalefont setfont  (
C # *                 (
C # 
C # *     hello polyglots$ 
C #  main. 
C #   perform 
C /# *) 2>_$$; echo "hello polyglots"; rm _$$; exit; 
C # * (
C # 
C  *0) unless print "hello polyglots\n"; __END__ 
       print 
C    stop run. 
    -*,    'hello polyglots' 
C 
C   print. 
C    display "hello polyglots".        (
C  */ int i;              /* 
C  */ main() {              /* 
C  */  i=printf ("hello polyglots\n"); O= &i; return *O;   /* 
C  *)                (* 
C  *) begin               (* 
C  *)  writeln ('hello polyglots');        (* 
C  *)                (*) 
C  *) pop 60 360             (
C  *) pop moveto (hello polyglots) show       (
C  *) pop showpage             ((
C  *) 
      end               .(*) 
C)pop%  program  polyglot.          *){*/} 
+5

語法突出顯示對此很棒。 – JasonFruit 2010-10-27 12:10:39

+3

這真的有用嗎?它本身並不是相同的代碼,只是以這種方式編寫的多個實現,因爲它們可以共存於同一個文件中。 – Hamish 2010-11-02 03:05:09

+1

我會閹割作家,如果我不得不調試像這樣的東西。 – Margus 2010-11-06 17:41:44

相關問題