Erlang Central

Difference between revisions of "Hello World"

From ErlangCentral Wiki

m (Solution: omission)
(8 intermediate revisions by 2 users not shown)
Line 5:Line 5:
 
== Solution ==
 
== Solution ==
  
This is very easy in Erlang:
+
This is very impractical in Erlang, but here you go:
  
 
<code>
 
<code>
1> "Hello World!".
+
$ erl -noshell -eval 'io:fwrite("Hello, world!\n"), init:stop().'
"Hello World!"
+
Hello, world!
2> io:fwrite("Hello World!\n").
+
$
Hello World!
+
</code>
ok
+
 
 +
A more popular method (yaws uses this within a short
 +
shell script that manages arguments):
 +
 
 +
<code>
 +
$ cat hello.erl                                                                                       
 +
-module(hello).
 +
-export([start/0]).
 +
 
 +
start() ->
 +
    io:fwrite("Hello, world!\n").
 +
$ erlc hello.erl
 +
$ erl -noshell -s hello start -s init stop
 +
Hello, world!
 +
$
 +
</code>
 +
 
 +
Finally, you can use escript from R11B-4.  This is probably what
 +
you really want, but please read below before getting too disappointed with
 +
escript's start-up delay.
 +
 
 +
<code>
 +
$ cat hello                                                                                           
 +
#! /usr/bin/env escript
 +
main(_) -> io:fwrite("Hello, world!\n").
 +
$ ./hello
 +
Hello, world!
 +
$
 
</code>
 
</code>
  
 
== Discussion ==
 
== Discussion ==
  
Erlang provides an interactive prompt, called the Read-Eval-Print-Loop (REPL), that allows you to type some simple function definitions into Erlang and have them evaluated immediately. This allows for much more immediate feedback than compiled languages.
+
To explain the above, I quote Erik Naggum's comments on comp.lang.lisp
 +
 
 +
<code>
 +
  The famous C program is famous for its ablity to produce an executable
 +
  with a minimal amount of fuss.  It really shows off the Unix environment,
 +
  and not the language.  However, an executable is only a funtcion that
 +
  resides on disk in the Unix file sysem and which you call from the
 +
  shell's read-execute interactive loop.  Common Lisp systems offer their
 +
  own interactive loop, called the read-eval-print loop and do not
 +
  generally take part in the shell interactive loop the same way other
 +
  languages do.  Some find this a insurmountable obstacle to using Common
 +
  Lisp, others love it.  However, the code for the core program is simple:
 +
</code>
 +
 
 +
Erlang has a similar situation to that of Common Lisp (also: Smalltalk), although Erlang's is mitigated by its nodes, which are more easily thought of as little operating systems.  But to make it clear: ''In Erlang you do not start a program from the unix shell, let it run its course, and then have it exit.''  Rather, you start one or many nodes and run programs within them. But if you'd like an example to show off the Erlang environment rather than the language:
 +
 
 +
<code caption="in /foo/bar/baz/hello.erl">
 +
-module(hello).
 +
-export([hello/0]).
 +
 
 +
hello() -> io:fwrite("Hello, world!\n").
 +
</code>
 +
 
 +
<code caption="in a long-running Erlang node">
 +
(mischief@mordor)199> cd("/foo/bar/baz").
 +
/foo/bar/baz
 +
(mischief@mordor)200> c(hello).
 +
{ok,hello}
 +
(mischief@mordor)201> hello:hello().
 +
Hello, world!
 +
ok
 +
(mischief@mordor)202>
 +
</code>
 +
 
 +
Alternatively, you could compile hello.erl separately:
 +
 
 +
<code>
 +
$ erlc hello.erl
 +
</code>
 +
 
 +
<code caption="in a long-running Erlang node">
 +
(mischief@mordor)199> code:add_pathz("/foo/bar/baz").
 +
true
 +
(mischief@mordor)200> hello:hello().
 +
Hello, world!
 +
(mischief@mordor)201>
 +
</code>
  
This is the first program many C programmers write, but it isn't the best way to start programming Erlang. Erlang makes extensive use of the functional style and it is better to become acquainted with this style before delving into input and output.
+
This is a quicker cycle that it might seem, here, and there's much more of interest in the environment than shown off, here. Just know that many people consider that the advantages of Erlang far outweigh not having the cute little unix executables celebrated by 'hello world' examples. This ''does'' constrain the sort of programs that you can sensibly write in Erlang, but so also are other languages constrained by not having Erlang nodes :-)
  
[[Category:CookBook]]
+
[[Category:CookBook]][[Category:Common Idioms]]

Revision as of 08:45, 4 April 2007

Problem

You want to write the classic "Hello World!" program in Erlang.

Solution

This is very impractical in Erlang, but here you go:

$ erl -noshell -eval 'io:fwrite("Hello, world!\n"), init:stop().'
Hello, world!
$

A more popular method (yaws uses this within a short shell script that manages arguments):

$ cat hello.erl                                                                                         
-module(hello).
-export([start/0]).

start() ->
    io:fwrite("Hello, world!\n").
$ erlc hello.erl 
$ erl -noshell -s hello start -s init stop
Hello, world!
$

Finally, you can use escript from R11B-4. This is probably what you really want, but please read below before getting too disappointed with escript's start-up delay.

$ cat hello                                                                                             
#! /usr/bin/env escript
main(_) -> io:fwrite("Hello, world!\n").
$ ./hello
Hello, world!
$ 

Discussion

To explain the above, I quote Erik Naggum's comments on comp.lang.lisp

  The famous C program is famous for its ablity to produce an executable
  with a minimal amount of fuss.  It really shows off the Unix environment,
  and not the language.  However, an executable is only a funtcion that
  resides on disk in the Unix file sysem and which you call from the
  shell's read-execute interactive loop.  Common Lisp systems offer their
  own interactive loop, called the read-eval-print loop and do not
  generally take part in the shell interactive loop the same way other
  languages do.  Some find this a insurmountable obstacle to using Common
  Lisp, others love it.  However, the code for the core program is simple: 

Erlang has a similar situation to that of Common Lisp (also: Smalltalk), although Erlang's is mitigated by its nodes, which are more easily thought of as little operating systems. But to make it clear: In Erlang you do not start a program from the unix shell, let it run its course, and then have it exit. Rather, you start one or many nodes and run programs within them. But if you'd like an example to show off the Erlang environment rather than the language:

-module(hello).
-export([hello/0]).

hello() -> io:fwrite("Hello, world!\n").
(mischief@mordor)199> cd("/foo/bar/baz").
/foo/bar/baz
(mischief@mordor)200> c(hello).
{ok,hello}
(mischief@mordor)201> hello:hello().
Hello, world!
ok
(mischief@mordor)202>

Alternatively, you could compile hello.erl separately:

$ erlc hello.erl
(mischief@mordor)199> code:add_pathz("/foo/bar/baz").
true
(mischief@mordor)200> hello:hello().
Hello, world!
(mischief@mordor)201>

This is a quicker cycle that it might seem, here, and there's much more of interest in the environment than shown off, here. Just know that many people consider that the advantages of Erlang far outweigh not having the cute little unix executables celebrated by 'hello world' examples. This does constrain the sort of programs that you can sensibly write in Erlang, but so also are other languages constrained by not having Erlang nodes :-)