Erlang (programming language)/Tutorials: Difference between revisions
imported>Eric Evers |
imported>Eric Evers (→regexp) |
||
Line 29: | Line 29: | ||
===lists=== | ===lists=== | ||
===regexp=== | ===regexp=== | ||
=Regular Expressions= | |||
2> regexp:match("hello world","w.+d"). | |||
{match,7,5} | |||
The regular expression, "w.+d" matches the string, "hello world" at location 7 for 5 chars. | |||
9> regexp:sub("10203040","([2|3|4]0)+","01"). | |||
{ok,"1001",1} | |||
Makes a substitution, replacing "203040" with "01". | |||
==Examples== | ==Examples== |
Revision as of 18:13, 19 April 2008
Erlang Language Programming Tutorials
Overview
Syntax of functions
Functions are defined by the domain of the arguments and the number of arguemnts. A function ends with a period. A function defined over differnt domains are separated by semicolons. A fact function gives an answer that is sensitive to the domain of the input. With strings it gives a fact. With counting numbers it gives the factorial function.
fact("aloha") -> "Aloha is a greating"; fact(String) when is_a_list(String) -> "no fact is known about " ++ String; fact(0) -> 1; fact(N) when is_integer(N) and (N > 0) -> fact(N-1)*N; fact(N) when N < 0 -> error.
Macros
-define(LIKERT_SCALE, lists:seq(1, 5)).
A = LIKERT_SCALE.
Simple Types
Advanced Types
Popular Modules
lists
regexp
Regular Expressions
2> regexp:match("hello world","w.+d"). {match,7,5}
The regular expression, "w.+d" matches the string, "hello world" at location 7 for 5 chars.
9> regexp:sub("10203040","([2|3|4]0)+","01"). {ok,"1001",1}
Makes a substitution, replacing "203040" with "01".
Examples
Hello World (serial)
Code Example
-module(hello). -export([start/0]). start() -> io:format("Hello, world!\n").
Analysis of the example
The Hello World program (see above) appears in many programming languages books and articles as a cursory introduction into a language's syntax. The first hello world program was introduced in the book The C Programming Language[1].
-module(hello)
tells the compiler to create a new module(library) called hello. The code tells us the file name for this code: hello.erl.
-export([start/0]).
exports a function named start with 0 arguments to the world outside of this module called hello.
start() ->
tells the compiler that there is a function named start() with no arguments.
io:format("Hello, world!\n").
will make the program output Hello, world!
and a new line (\n
) on the screen.
Hello World (parallel)
Prime Sieve (parallel with linda type coordination)
Autonomous Agents
See definition of Autonomous Agent.
Advanced OTP
ETS
Mimsia
References
1 - Erlang Man Pages at Erlang,org
- ↑ Cite error: Invalid
<ref>
tag; no text was provided for refs namedK&R