Empty program: Difference between revisions
Simpler D entry |
|||
Line 598: | Line 598: | ||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
An empty text file is a correct Powershell script that does nothing. |
An empty text file is a correct Powershell script that does nothing. |
||
=={{header|PSQL}}== |
|||
EXECUTE BLOCK |
|||
RETURNS(S VARCHAR(40)) |
|||
AS |
|||
BEGIN |
|||
S = 'Hello World!'; |
|||
SUSPEND; |
|||
END |
|||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
Revision as of 10:42, 28 July 2011
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
In this task, the goal is to create the simplest possible program that is still considered "correct."
Programming Languages
Ada
<lang ada>procedure Empty is begin
null;
end;</lang>
Agena
Actually nothing is valid code, too. <lang agena></lang>
ALGOL 68
Brief form
<lang algol68>~</lang>
BOLD form
<lang algol68>SKIP</lang>
AmigaE
<lang amigae>PROC main() ENDPROC</lang>
AppleScript
An empty .scpt file is considered the smallest runnable code, but the following would also be acceptable. <lang applescript>return</lang>
Argile
The empty string or file are valid and do nothing. <lang Argile></lang>
AutoHotkey
An empty script would be enough. Adding "#Persistent" makes it persistent. <lang AutoHotkey>#Persistent </lang>
AutoIt
An a single comment can be considered a valid program that does nothing. <lang AutoIt>;nothing</lang>
AWK
The empty string (or file) is recognised as valid program that does nothing.
The program
<lang awk> 1</lang>
is the simplest useful program: like the UNIX command 'cat', it prints every line of the files given as arguments, or (if no arguments are given) the standard input.
BASIC
<lang qbasic>10 END</lang>
On the ZX Spectrum, we can have a completely empty program with no lines. Here we attempt to run the empty program:
RUN 0 OK, 0:1
Batch File
An empty batch file is syntactically correct and does nothing.
BBC BASIC
In BBC BASIC an empty program is syntactically correct. <lang bbcbasic></lang>
Befunge
@
The halt command @ is required because code wraps around. An empty file would be an infinite loop.
Brainf***
Empty program
Note: this works as all non-instruction characters are considered comments. Alternatively, a zero-byte file also works.
Brlcad
Pressing enter from the mged prompt, just returns another prompt, so I suppose that is the smallest possible program. However, before we can draw anything we at least need to open a database:
<lang mged>opendb empty.g y</lang>
C
You'll notice that each example has an int return type, and returns 0. The meaning of the return value is system-dependent.
argc contains the number of arguments. argv is an array containing those arguments as null-terminated strings. These memory for these strings are maintained by the runtime, and don't need to be manually freed. <lang c>int main(int argc, char* argv[]) {
return 0;
} </lang>
With some compilers, argc and argv may omitted.
<lang c>int main () {
return 0;
}</lang>
<lang c>int main() {
initscr(); /* Initialize terminal to curses mode. */
/* Code that depends on curses mode would go here. */
endwin(); /* End curses mode. */
return 0;
} </lang>
C#
<lang csharp>class Program {
static void Main() { }
}</lang>
C++
<lang cpp>int main ( int /*argc*/, char * * /*argv*/ ) {
// Unused arguments should not be named // There are variations: // 1: main may explicitly return a value // (other non-void-returning C++ functions must do so, // but there's a special exception for main that falling off it // without an explicit return is equivalent to a "return 0;" at // the end of the main function) // 2: The arguments may be omitted entirely
}</lang>
Clean
module Empty Start world = world
Compile the project with No Console or No Return Type to suppress printing of the value of the world.
Clojure
An empty file is the simplest valid Clojure program.
D
<lang d>void main() {}</lang>
Delphi
- See Pascal
E
The shortest possible program:
This is equivalent to:
null
eC
or
class EmptyApp : Application { void Main() { } }
Eiffel
A file called main.e: <lang eiffel>class MAIN
create main feature main do end
end</lang>
Erlang
An empty module:
-module(empty).
An empty Erlang script file (escript):
main(_) -> 1.
eSQL
CREATE COMPUTE MODULE ESQL_Compute CREATE FUNCTION Main() RETURNS BOOLEAN BEGIN RETURN TRUE; END; END MODULE;
Euphoria
<lang Euphoria></lang>
F#
F# has an interactive mode and a compiled mode. The interactive interpreter will accept an empty file so the shortest valid program is an empty zero-length file with the .fsx extension. <lang fsharp></lang> An empty compiled program is: <lang fsharp>[<EntryPoint>] let main args = 0</lang>
Factor
<lang factor></lang>
If you want to deploy a stand-alone application, that doesn't suffice though. Here's another version.
<lang factor>IN: rosetta.empty
- main ( -- ) ;
MAIN: main</lang>
Falcon
>
Prints an empty line.
>>
Prints nothing.
FALSE
Fantom
<lang> class Main {
public static Void main () {}
} </lang>
Forth
<lang forth></lang> For a Forth script to be used from a shell, you usually want the last command to be BYE in order to exit the interpreter when finished. <lang forth>bye</lang>
Fortran
end
Frink
Empty programs are valid. <lang frink> </lang>
Gema
An empty program will copy input stream to output stream unchanged. <lang gema></lang>
Genyris
Go
<lang go>package main func main() { }</lang>
Groovy
Haskell
Standard: Haskell 98
The simplest possible program is a single module using the implicit module header "module Main(main) where", and defining the action main to do nothing:
main = return ()
The simplest possible module other than Main is one which contains no definitions:
module X where {}
haXe
class Program { static function main() { } }
Unlike most languages haXe doesn't have arguments in the main function because it targets different platforms (some which don't support program arguments, eg: Flash or Javascript). You need to use the specific libraries of the platform you are targeting to get those.
HicEst
<lang hicest>END ! looks better, but is not really needed</lang>
Icon and Unicon
<lang Icon>procedure main() # a null file will compile but generate a run-time error for missing main end</lang>
IDL
end
Intercal
<lang intercal>PLEASE GIVE UP</lang>
Io
J
<lang j></lang> It returns itself: <lang j> -: ". 1 </lang>
Java
<lang java>public class EmptyApplet extends java.applet.Applet {
@Override public void init() { }
}</lang>
<lang java>public class EmptyMainClass {
public static void main(String... args) { }
}</lang>
The "..." basically means "as many of these as the programmer wants." Java will put multiple arguments into an array with the given name. This will work for any method where an array is an argument, but with a twist. A call can be made like this:
<lang java>method(arg0, arg1, arg2, arg3)</lang>
All of the args will be put into an array in the order they were in the call.
<lang java>public class EmptyMainClass {
public static void main(String[] args) { }
}</lang>
<lang java>public class EmptyApplet extends java.applet.Applet {
public void init() { }
}</lang>
@Override - Indicates that a method declaration is intended to override a method declaration in a superclass. If a method is annotated with this annotation type but does not override a superclass method, compilers are required to generate an error message. It's present from JDK 5.0 (1.5.0) and up.
Actually this is not strictly correct. The smallest possible correct program in Java is an empty source file.
JavaScript
The empty file is a valid program.
Joy
<lang joy>
- !/usr/local/bin/joy.exe
quit. </lang>
The RCE(P)L of JOY reads a program -- which looks like a sentence, ending in a full stop -- compiles it, executes it and optionally prints the top of the stack. The only way to exit this loop properly is to call quit, which is equivalent to exit(0); in C.
KonsolScript
<lang KonsolScript>function main() {
}</lang>
Liberty BASIC
<lang lb>end</lang>
Lisp
Most Lisp dialects, including Common Lisp, will accept no text (no forms) as a valid program. <lang lisp></lang>
Logo
<lang logo></lang> or end a standalone script with "bye" <lang logo>
- ! /usr/local/bin/logo
bye </lang>
LSE64
As with Forth, an empty file is the shortest program. To exit the interpreter at the end of a loaded file:
bye
Lua
<lang Lua></lang>
M4
<lang M4></lang>
Mathematica
<lang Mathematica></lang>
MATLAB
<lang Matlab> function [varargout] = emptyprogram(varargin) </lang>
MAXScript
An empty MAXScript file returns "OK" on execution
Metafont
<lang metafont>end</lang>
MMIX
<lang mmix> LOC #100 Main TRAP 0,Halt,0 // main (argc, argv) {}</lang>
Modula-2
<lang modula2>MODULE Main;
BEGIN END Main.</lang>
Modula-3
<lang modula3>MODULE Main;
BEGIN END Main.</lang>
MUMPS
The empty file is a valid program.
Nemerle
Compiles with warnings:<lang Nemerle>null</lang> Compiles without warnings (so, more correct):<lang Nemerle>module Program {
Main() : void { }
}</lang>
NetRexx
The following two samples both generate valid programs.
This minimal example requires that the file be named to match the class: <lang NetRexx>class empty</lang>
This example will generate it's class based on the file name: <lang NetRexx>method main(args = String[]) static</lang>
NewLISP
<lang NewLISP>; </lang>
Nimrod
Objeck
<lang objeck> bundle Default {
class Empty { function : Main(args : String[]) ~ Nil { }
} </lang>
Objective-C
<lang objc>int main(int argc, const char **argv) {
return 0;
}</lang>
The minimal empty Cocoa/OpenStep application, useful as life-support for many examples given at RosettaCode, is
<lang objc>#import <Cocoa/Cocoa.h>
int main( int argc, const char *argv[] ) {
NSAutoreleasePool *pool; pool = [[NSAutoreleasePool alloc] init]; NSApp = [NSApplication sharedApplication]; [pool release]; return 0;
}</lang>
OCaml
<lang ocaml>;;</lang>
Actually, the smallest possible correct program in OCaml is an empty source file.
Octave
An empty text file can be a valid empty program, but since Octave has the concept of "function file" (a file containing a single function; the file is automatically loaded when a function with the same name of the file, save for the extension, is called, and the first function present in the file is used), the name of the empty file matters. E.g. calling an empty file as isempty.m makes unusable the builtin isempty function.
Oz
REPL
The simplest program that can be evaluated in the REPL (Emacs mode) is the empty program. <lang oz></lang>
Accepted by compiler
The simplest 'program' that can be compiled is a file which contains a single expression. <lang oz>unit</lang> Such a 'program' cannot be executed, though.
Standalone
The simplest standalone program is a root functor that does not define anything. ("Functors" are first-class modules.) <lang oz>functor define
skip
end</lang>
PARI/GP
<lang parigp></lang>
Pascal
<lang pascal>program ProgramName;
begin end.</lang>
The first line is not necessary in modern Pascal dialects. With today's most compilers, the empty program is just: <lang pascal>begin end.</lang>
Perl
<lang perl>#!/usr/bin/perl 1;</lang>
<lang perl>#!/usr/bin/perl exit;</lang>
<lang perl>#!/usr/bin/perl
- A program without a body will work too</lang>
<lang perl>#!/usr/bin/perl</lang>
The smallest possible program is an empty file (zero length). This requires you to specify the interpreter instead of relying on the shell's shebang magic, thus: perl empty.pl.
The smallest possible Perl one-liner is perl -e0.
Perl 6
In theory, an empty file is a valid Perl 6 program that does nothing. However, Synopsis 1 decrees "The perl interpreter will assume that it is being fed Perl 5 code unless the code starts with a 'class' or 'module' keyword, or you specifically tell it you're running Perl 6 code in some other way". So, a strictly specification-compliant implementation would interpret an empty file as Perl 5. Probably the shortest file that's unconditionally a Perl 6 program is
<lang perl6>use v6;</lang>
since it'll cause pre-6 perl
s to die with an appropriate error message.
PHP
An empty text file is a correct PHP program that does nothing.
PicoLisp
<lang PicoLisp>(de foo ())</lang>
PIR
The :main pragma indicates that a subroutine is the program's entry point. However, if a subroutine is the first (or only, which would also have the effect of making it the first) routine in the program, Parrot will use that. So we may comfortably omit it in this case.
<lang pir>.sub empty_program .end</lang>
Pike
<lang pike>int main(){}</lang>
PL/I
<lang PL/I> s: proc options (main); end; </lang>
PL/SQL
BEGIN NULL; END;
Pop11
Pop11 has two compilers, incremental and batch compiler. For the incremental compiler one can use just empty program text (empty file), or a file containing nothing but a comment, e.g.
;;; This is a valid Pop11 program that does absolutely nothing.
The batch compiler generates an executable which starts at a given entry point, so one should provide an empty function. If one wants program that works the same both with incremental compiler and batch compiler the following may be useful:
compile_mode :pop11 +strict; define entry_point(); enddefine;
#_TERMIN_IF DEF POPC_COMPILING entry_point();
Here the batch compiler will stop reading source before call to entry_point while incremental compiler will execute the call, ensuring that in both cases execution will start from the function entry_point.
PostScript
An empty file is a valid PostScript program that does nothing.
Following good programming practice, however, and to ensure that a PostScript printer will interpret a file correctly, one should make the first 4 characters of the file be
%!PS
If a particular version of the PS interpreter is needed, this would be included right there:
%!PS-2.0 % ...or... %!PS-3.0 % etc
PowerShell
An empty text file is a correct Powershell script that does nothing.
PSQL
EXECUTE BLOCK RETURNS(S VARCHAR(40)) AS BEGIN S = 'Hello World!'; SUSPEND; END
PureBasic
An empty file is a correct PureBasic program that does nothing. <lang PureBasic></lang>
Python
An empty text file is a correct Python program that does nothing.
R
An empty text file is a valid empty program
Raven
An empty text file is an empty program.
REBOL
The header section is mandatory if you want it to be recognized as a REBOL program. It doesn't have to be filled in though: <lang REBOL>REBOL []</lang>
Retro
An empty file is the smallest valid program.
<lang Retro> </lang>
To save an application doing nothing, you need to do a bit more work though:
<lang Retro> &bye is boot </lang>
REXX
version 1
An empty file is a valid empty program; however, for some REXX implementation the first two characters must be /* (i.e. the first line must be a comment) in order to recognize the file as a REXX script: <lang rexx> /* empty */ </lang>
version 2
<lang rexx> /**/ </lang>
Rhope
Main(0,0) |: :|
Ruby
An empty file is a valid Ruby program. However, in order to make it runnable on *nix systems, a shebang line is necessary: <lang ruby>#!/usr/bin/env ruby</lang>
Scala
<lang scala>object emptyProgram {
def main(args: Array[String]) {}
}</lang>
Scheme
<lang scheme></lang>
Seed7
<lang seed7>$ include "seed7_05.s7i";
const proc: main is noop;</lang>
Slate
<lang slate> </lang>
SNUSP
$#
$ sets the instruction pointer (going right), and # halts the program (empty stack).
SNOBOL4
A valid program requires an end label. The shortest (virtually empty) program is then: <lang snobol>end</lang>
Standard ML
<lang sml>;</lang>
Actually, the smallest possible correct program in Standard ML is an empty source file.
Suneido
<lang Suneido>function () { }</lang>
Tcl
Nothing is mandatory in Tcl, so an empty file named nothing.tcl would be a valid "empty program".
Toka
For interpreted code, nothing is required, although bye is necessary for an empty script to exit (rather than wait for the user to exit the listener). Hence:
bye
Or, for a directly runnable script:
#! /usr/bin/toka bye
For compiled code, the simplest program is an empty quote:
[ ]
Again, to exit the listener, you will still need user input if this is not followed with bye.
TI-83 BASIC
:
TI-89 BASIC
Prgm EndPrgm
Trith
<lang trith></lang>
TUSCRIPT
<lang tuscript> $$ MODE TUSCRIPT </lang>
UNIX Shell
#!/bin/sh
#!/bin/bash
Unlambda
i
(See how i plays so many roles in unlambda?)
VBScript
An empty .vbs file is considered the smallest runnable code, but the following (a single apostrophe as comment marker) would also be acceptable (along with other non-executing instructions like option explicit
.)
<lang vb>'</lang>
Visual Basic .NET
<lang vbnet>Module General
Sub Main() End Sub
End Module</lang>
X86 Assembly
<lang asm> section .text global _start
_start: mov eax, 1 int 0x80 ret
</lang>
<lang asm> .386 .model flat, stdcall option casemap:none
.code start: ret end start </lang>
XQuery
<lang xquery>.</lang> The dot selects the current context node and returns it unchanged.
XSLT
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> </xsl:stylesheet>
Add other namespaces to the stylesheet attributes (like xmlns:fo="http://www.w3.org/1999/XSL/Format") if you use them.
Since XSLT is XML, and <lang>transform</lang> is a synonym for <lang>stylesheet</lang>, the example above can be minified to:
<transform xmlns="http://www.w3.org/1999/XSL/Transform" version="1.0"/>
This stylesheet echoes the text content of an XML file. The shortest stylesheet without any output would be
<transform xmlns="http://www.w3.org/1999/XSL/Transform" version="1.0"> <template match="/" /> </transform>
xTalk
on startup end startup
Yorick
An empty file is valid and does nothing.
Markup Languages
HTML
<lang html><HTML></HTML></lang>
Plain TeX
<lang tex> \bye </lang>
LaTeX
<lang latex>\documentclass{article} \begin{document} \end{document}</lang>
- Programming Tasks
- Basic language learning
- Ada
- Agena
- ALGOL 68
- AmigaE
- AppleScript
- Argile
- AutoHotkey
- AutoIt
- AWK
- BASIC
- Batch File
- BBC BASIC
- Befunge
- Brainf***
- Brlcad
- C
- Ncurses
- C sharp
- C++
- Clean
- Clojure
- D
- Delphi
- E
- EC
- Eiffel
- Erlang
- ESQL
- Euphoria
- F Sharp
- Factor
- Falcon
- FALSE
- Fantom
- Forth
- Fortran
- Frink
- Gema
- Genyris
- Go
- Groovy
- Haskell
- HaXe
- HicEst
- Icon
- Unicon
- IDL
- Intercal
- Io
- J
- Java
- JavaScript
- Joy
- KonsolScript
- Liberty BASIC
- Lisp
- Common Lisp
- Logo
- LSE64
- Lua
- M4
- Mathematica
- MATLAB
- MAXScript
- Metafont
- MMIX
- Modula-2
- Modula-3
- MUMPS
- Nemerle
- NetRexx
- NewLISP
- Nimrod
- Objeck
- Objective-C
- OCaml
- Octave
- Oz
- PARI/GP
- Pascal
- Perl
- Perl 6
- PHP
- PicoLisp
- PIR
- Pike
- PL/I
- PL/SQL
- Pop11
- PostScript
- PowerShell
- PSQL
- PureBasic
- Python
- R
- Raven
- REBOL
- Retro
- REXX
- Rhope
- Ruby
- Scala
- Scheme
- Seed7
- Slate
- SNUSP
- SNOBOL4
- Standard ML
- Suneido
- Tcl
- Toka
- TI-83 BASIC
- TI-89 BASIC
- Trith
- TUSCRIPT
- UNIX Shell
- Unlambda
- VBScript
- Visual Basic .NET
- X86 Assembly
- XQuery
- XSLT
- XTalk
- Yorick
- HTML
- PlainTeX
- LaTeX