Friday, February 14, 2014

Infinite Output in Different Languages

I enjoy the Olimex Weekend Programming Challenge. I missed #33 but it's one of my favorites so far: generate infinite output with the smallest programs possible in different languages. How many languages can you do it in?


Above is how you'd enter the code in Commodore 64 BASIC (the listing would expand the keywords), The ? is PRINT, the G shift-O (right angle character) is GOTO.

Here are the solutions Olimex received. I've selected a few interesting solutions and added some of my own:


brainf***
+[.]

This is a cool language. Utterly useless, but cool. And this is a family-friendly blog so I'm not spelling out the name.

python
while(1):
     print "."

Perl
print 1 while 1;


I program Perl with a thick C accent, so I didn't know you could do this.

lisp
(loop(princ 1))

I haven't written anything lispy in years. I doubt I ever wrote a loop in that language.

Here's some other languages that weren't submitted...

Pascal
Program z(output);
begin
    while true
    do begin
        writeln('x');
    end;
end.

Yeah, a bit wordy. That's Pascal for ya. Haven't written Pascal since the late 80's during my first CS class in College.

FORTRAN
PROGRAM Z
   1 PRINT *, "x"
   GO TO 1
END PROGRAM Z

Less wordy. I don't know why but I love Fortran. It just seems so simple and transparent.

Lua
repeat
     print("x")
until false

I'm curious about Lua. It seems neat and is supposed to be lightweight and great for embedded microcontrollers.

RedCode
MOV 0, 1

This is code used in CoreWar. It doesn't output to a screen. RedCode can only modify the core's memory. Although the RedCode VM displays memory as output so I guess you could argue... but I'll spare us all.

DCL
$ loop:
$      write sys$output "x"
$ goto loop

Or something like that. I couldn't find an online DCL interpreter and I'm fresh out of OpenVMS systems to test on. I used to write a ton of DCL scripts. I grew up on VMS in college before migrating to Unix.

Icon
procedure main()
 repeat write ( 'x' );
end

I learned a bit of Icon at the University of Arizona where it was developed. It evolved from SNOBOL.

Alright, your turn. What obscure languages can you throw at me?

Can you think of any languages that don't support infinite loops? I think APL might be one.

Post in the comments...

7 comments:

  1. Hi there. If you liked brainf*** you will love befunge, the infinite output in this language is also pretty neat :

    .

    Yep, a single point will output zeros until the end of times.

    ReplyDelete
  2. Whitespace would be another fun language to program infinite output in.

    ReplyDelete
  3. I don't really see what's interesting about the Python one. Also you can make it shorter by getting rid of the quotes.

    ReplyDelete
    Replies
    1. Fair enough. Now, to make things more interesting, how about an example of your own? :)

      Delete
  4. Er, what I should have said is you can replace "." with 1 to make the Python example shorter.

    ReplyDelete
  5. C/C++
    main(){for(;;)puts("1");}

    CSH
    while(1)
    echo 2
    end


    Fortran 90

    PROGRAM X
    DO WHILE(1)
    WRITE(*,*)'3'
    ENDDO
    END

    ReplyDelete
  6. Pascal:
    Program z;
    begin
    while true do
    write('x');
    end.

    or
    Program z;
    begin
    repeat
    write('x');
    until false;
    end.

    or if you like bad programming structure even ...
    Program z;
    begin
    a: write('x');
    goto a;
    end.

    Arduino:
    void setup() { Serial.begin(9600);}
    void loop() { Serial.write(1); }

    Javascript: (html tag not allowed in this comment box, so tag modified)
    html>
    body>
    script type="text/javascript">
    i=0 while (i<1)
    {
    document.write("1")
    }
    /script>
    /body>
    /html>

    ReplyDelete

Note: Only a member of this blog may post a comment.