Hello world/Newline omission

From Rosetta Code
Revision as of 02:26, 25 October 2011 by 192.139.122.42 (talk) (Omit from TXR.)
Task
Hello world/Newline omission
You are encouraged to solve this task according to the task description, using any language you may know.

Some languages automatically insert a newline after outputting a string, unless measures are taken to prevent its output. The purpose of this task is to output the string "Goodbye, World!" preventing a trailing newline from occuring.

See also

Ada

<lang ada> with Ada.Text_IO;

procedure Goodbye_World is begin

  Ada.Text_IO.Put("Goodbye, World!");

end Goodbye_World; </lang>

BASIC

<lang basic>10 REM The trailing semicolon prevents a newline 20 PRINT "Goodbye, World!";</lang>

C

In C, we do not get a newline unless we embed one: <lang c>#include <stdio.h>

int main(int argc, char *argv[]) {

 (void) printf("Goodbye, World!");    /* No automatic newline */
 return EXIT_SUCCESS;

}</lang>

However ISO C leaves it up to implementations to define whether or not the last line of a text stream requires a new-line. This means that the C can be targetted to environments where this task is impossible to implement, at least with a direct text stream manipulation like this.

C++

In C++, using iostreams, portable newlines come from std::endl. Non-portable newlines may come from using constructs like \n, \r or \r\n. If we don't use any of these, we won't get a newline. <lang cpp>#include <iostreams>

int main(int argc, char *argv[]) {

 std::cout << "Goodbye, World!";

}</lang>

C#

<lang csharp>using System;

class Program {

   static void Main (string[] args) {
       //Using Console.WriteLine() will append a newline
       Console.WriteLine("Goodbye, World!");
       //Using Console.Write() will not append a newline
       Console.Write("Goodbye, World!");
   }

}</lang>

D

Works with: D version 2.0

<lang D>import std.stdio;

void main() {

   write("Goodbye, World!");

}</lang>

Euphoria

<lang euphoria>-- In Euphoria puts() does not insert a newline character after outputting a string puts(1,"Goodbye, world!")</lang>

Factor

<lang factor>USE: io "Goodbye, World!" write</lang>

Forth

<lang Forth>\ The Forth word ." does not insert a newline character after outputting a string ." Goodbye, world!"</lang>

F#

<lang fsharp> // A program that will run in the interpreter (fsi.exe) printf "Goodbye, World!"

// A compiled program [<EntryPoint>] let main args =

   printf "Goodbye, World!"
   0 

</lang>

gecho

<lang gecho>'Hello, <> 'world! print</lang>

Go

<lang go>package main

import "fmt"

func main() { fmt.Print("Goodbye, World!") }</lang>

GUISS

In Graphical User Interface Support Script, we specify a newline, if we want one. The following will not produce a newline: <lang GUISS>Start,Programs,Accessories,Notepad,Type:Goodbye World[pling]</lang>

J

Interpreting this task in the context of J is problematic. For example, many people use J from a browser where newlines are omitted by default. Meanwhile the J interpreter prompt always begins on a new line, so in interactive use this task becomes meaningless. Further, J provides strong tools for coalescing results and manipulating them prior to output, so newline elimination would typically happen before output rather than after.

Nevertheless, text 1!:3 filereference will append to the referenced file without inserting any extra newlines (which might be /proc/self/stdout on linux -- though of course stdout would typically not be meaningful if J is running in the browser and is also of dubious usefulness if J is running in GTK -- still, it can be done, even if it's meaningless).

So, if a J programmer were asked to solve this task, the right approach would be to ask why that is needed, and then solve the real problem instead. 1!:3 might or might not be the right answer, depending on the real issue.

Haskell

<lang haskell>main = putStr "Goodbye, world"</lang>

Icon and Unicon

Native output in Icon and Unicon is performed via the write and writes procedures. The write procedure terminates each line with both a return and newline (for consistency across platforms). The writes procedure omits this. Additionally, the programming library has a series of printf procedures as well. <lang Icon>procedure main()

  writes("Goodbye, World!")    

end</lang>

Java

<lang java>public class HelloWorld {

public static void main(String[] args)
{
 System.out.print("Goodbye, World!");
}

}</lang>

Liberty BASIC

A trailing semicolon prevents a newline <lang lb>print "Goodbye, World!"; </lang>


ML/I

Simple solution

In ML/I, if there isn't a newline in the input, there won't be one in the output; so a simple solution is this (although it's hard to see that there isn't a newline). <lang ML/I>Goodbye, World!</lang>

More sophisticated solution

To make it clearer, we can define an ML/I skip to delete itself and an immediately following newline. <lang ML/I>MCSKIP " WITH " NL Goodbye, World!""</lang>

Nemerle

<lang Nemerle>using System.Console;

module Hello {

   // as with C#, Write() does not append a newline
   Write("Goodbye, world.");
   // equivalently
   Write("Goodbye, ");
   Write("world.");

}</lang>

Objeck

<lang objeck> bundle Default {

 class SayHello {
   function : Main(args : String[]) ~ Nil {
     "Hello World!"->Print();
   }
 }

} </lang>

OCaml

In OCaml, the function print_endline prints a string followed by a newline character on the standard output and flush the standard output. And the function print_string just prints a string with nothing additional.

<lang ocaml>print_string "Goodbye, World!"</lang>

PARI/GP

<lang parigp>print1("Goodbye, World!")</lang>

PASM

<lang pasm>print "Goodbye World!" # Newlines do not occur unless we embed them end</lang>

Perl

<lang perl>print "Goodbye, World!"; # A newline does not occur automatically</lang>

Perl 6

A newline is not added automatically to print or printf <lang perl6>print "Goodbye, World!"; printf "%s", "Goodbye, World!";</lang>

PicoLisp

<lang PicoLisp>(prin "Goodbye, world")</lang>

PureBasic

<lang PureBasic>OpenConsole() Print("Goodbye, World!") Input() ;wait for enter key to be pressed</lang>

Python

<lang python>import sys sys.stdout.write("Goodbye, World!")</lang>

Works with: Python version 3.x

<lang python>print("Goodbye, World!", end="")</lang>

Retro

<lang Retro>"Goodbye, World!" puts</lang>

Ruby

<lang ruby>print "Goodbye, World!"</lang>

Scheme

<lang scheme>(display "Goodbye, World!")</lang>

Seed7

<lang seed7>$ include "seed7_05.s7i";

const proc: main is func

 begin
   write("Goodbye, World!");
 end func;</lang>

Standard ML

<lang sml>print "Goodbye, World!"</lang>

Tcl

<lang tcl>puts -nonewline "Goodbye, World!"</lang>

UNIX Shell

The echo command is not portable, and echo -n is not guaranteed to prevent a newline from occuring. With the original Bourne Shell, echo -n "Goodbye, World!" prints -n Goodbye, World! with a newline. So use a printf instead.

Works with: Bourne Shell

<lang bash>printf "Goodbye, World!" # This works. There is no newline. printf %s "-hyphens and % signs" # Use %s with arbitrary strings.</lang>

The print command, from the Korn Shell, would work well, but most shells have no print command. (With pdksh, print is slightly faster than printf because print runs a built-in command, but printf forks an external command. With ksh93 and zsh, print and printf are both built-in commands.)

Works with: ksh93
Works with: pdksh
Works with: zsh

<lang bash>print -n "Goodbye, World!" print -nr -- "-hyphens and \backslashes"</lang>

C Shell

C Shell does support echo -n and omits the newline.

<lang csh>echo -n "Goodbye, World!" echo -n "-hyphens and \backslashes"</lang>

ZX Spectrum Basic

<lang basic>10 REM The trailing semicolon prevents a newline 20 PRINT "Goodbye, World!";</lang>