corner
Home

· About erlang.org
· Downloads
· Links
· FAQs + mailing lists
· Enhancements
· Getting started
· Documentation
· Examples
· Mirrors

Erlang Books









For comments or questions about this site, contact webmaster@erlang.org
     

Small example programs

This directory contains a number of small programs. Each program is contained in a single file. Only modules in the standard distribution are used. Each program has a short description and a boxed example showing which commands must be given in order to run the program.

getty.erl A simple telnet client
urlget.erl get a URL
webput.erl Copy contents of local directory to web site
board.erl A ball rolling game
find.erl Find in Erlang
count_x.erl Count the number of x's in a file
tetris.erl Parallel tetris


getty.erl

A simple telnet client

This program is an example of a TCP server that waits for a TCP connection on a given port (default 7788). When a connection is established the port is used as a remote Erlang session.

Be aware that this of course gives anyone access to your system, it is only intended as an example!

To start the program:


1> getty:start().
<0.31.0>

      

This sets up client that listens to port 7788.

We now move to another machine in the network and try to connect to this port:



> telnet gordons 7788
Trying 150.236.14.77...
Connected to gordons.
Escape character is '^]'.

Welcome to Erlang!

Eshell V4.7.3  (abort with ^G)
1>

      


urlget.erl

get a URL

This module exports a couple of routines for fetching URL either directly or through a proxy. Example:


> urlget:direct("http://www.ericsson.se/cslab/~joe").       
progress:#bytes = 223
progress:#bytes = 229
progress:#bytes = 224
progress:#bytes = 1289
progress:#bytes = 2313
progress:#bytes = 2749
progress:#bytes = 3773
progress:#bytes = 3925
progress:#bytes = 4949
progress:#bytes = 5385
progress:#bytes = 5534
{ok,{"http://www.ericsson.se:800/cslab/~joe/",
     {"HTTP/1.0",200,"OK"},
     [{"Last-modified","Thu, 11 Jun 1998 14:32:05 GMT"},
      {"Content-length","5534"},
      {"Content-type","text/html"},
      {"Server","Apache/1.1b4"},
      {"Date","Thu, 12 Nov 1998 14:06:21 GMT"}],
     "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2//EN\">\n<html>\n<head>\n
	<title>Joe Armstrong's home page</title>\n</head>
... many lines omitted ...

      


webput.erl

Copy contents of local directory to web site

This program uses the library module ftp to automate publication of data on a remote web site.


< webput:publish("bingo.baz.dobedo.se","jimbo23","waX2p34",
	       "/home/joe/html/mirror",
	       "/pub/acct2754/html").
ok

      
The above command does the following:

  1. FTP's to the host bingo.baz.dobedo.se
  2. Logs in as user jimbo23 with password waX2p34.
  3. Copies all the files in the local directory /home/joe/html/mirror to /pub/acct2754/html

board.erl

A ball rolling game

An amazingly exciting game instructions here , to start:


> board:start().

      


find.erl

Find in Erlang

find.erl can also be found in Klacke's example collection, here it is again in case you missed it. Here's a couple of examples:


> find:files(".",".*\.erl$", fun(X) -> X end).
["./getty.erl",
 "./urlget.erl",
 "./geturl.erl",
 "./roll.erl",
 "./geturl.sockets.erl",
 "./webput.erl",
 "./find.erl"].

      
Now we can have some fun:


1> Size = fun(F) -> {ok,B}=file:read_file(F), size(B) end.        
#Fun<erl_eval>
2> Size("find.erl").  
1654
3> find:files(".",".*\.erl$", fun(X) -> {filename:basename(X),Size(X)} end). 
[{"getty.erl",2213},
 {"urlget.erl",6993},
 {"geturl.erl",6993},
 {"roll.erl",9961},
 {"geturl.sockets.erl",5542},
 {"webput.erl",1061},
 {"find.erl",1654}]

      
Command 1 creates a fun called Size which computes the size of a file. Command 2 tests it. Command 3 finds all Erlang files in the current directory and works out their size.


count_x.erl

Count the number of x's in a file



> count_x:file("count_x.erl").
4

      


tetris.erl

Parallel tetris

Runs N parallel versions of tetris, for example:


> tetris:start(3).

      
Starts three parallel tetri tetresuses?

 
Last updated   2010-11-29 13:15 UTC