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

Erlang Books

For comments or questions about this site, contact

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


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().


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
Connected to gordons.
Escape character is '^]'.

Welcome to Erlang!

Eshell V4.7.3  (abort with ^G)



get a URL

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

> urlget:direct("").       
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
     [{"Last-modified","Thu, 11 Jun 1998 14:32:05 GMT"},
      {"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 ...



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("","jimbo23","waX2p34",

The above command does the following:

  1. FTP's to the host
  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


A ball rolling game

An amazingly exciting game instructions here , to start:

> board:start().



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).

Now we can have some fun:

1> Size = fun(F) -> {ok,B}=file:read_file(F), size(B) end.        
2> Size("find.erl").  
3> find:files(".",".*\.erl$", fun(X) -> {filename:basename(X),Size(X)} end). 

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 the number of x's in a file

> count_x:file("count_x.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