erlang introduction bcberlin3
TRANSCRIPT
Erlang Introduction
1
2
3
4
5
6
7
8
Syntax
9
Hello User
10
erl
11
$ erl1> |
12
1> io:format("Hello User~n", []).Hello Userok
13
2> 1 + 2008.2009
14
3> 1 + 2 + 3.6
15
10> 1 + 2 * 3.711> 2 * 3 + 1.712> (1 + 2) * 3.9
16
13> 3/0.** exception error: bad argument in an arithmetic expression in operator '/'/2 called as 3 / 0
17
4> catch 3/0.{'EXIT',{badarith,[{erlang,'/',[3,0]}, {erl_eval,do_apply,5}, {erl_eval,expr,5}, {shell,exprs,6}, {shell,eval_exprs,6}, {shell,eval_loop,3}]}}
18
15> "Erlang intro"."Erlang intro"
19
35> "abc" ++ "def"."abcdef"
20
27> [72, 101, 108, 108, 111]."Hello"
21
27> [$E, $r, $l, $a, $n, $g]."Erlang"
22
Atom
23
6> atom.atom
24
oktruefalsekernelerlang'EXIT''Name'
25
6> is_atom(zbar).true
9> 1 == 3.false
26
Use Functions
27
36> lists:reverse("nilreB olleH")."Hello Berlin"
28
1> Mod = lists.lists2> Fn = reverse.reverse3> Mod:Fn("tac")."cat"
29
5> Mod:(list_to_atom("reverse"))("ate"). "eta"
30
37> apply(lists, reverse, ["nilreB"])."Berlin"
31
39> Fn = fun lists:reverse/1.#Fun<lists.reverse.1>40> apply(Fn, ["olleH"])."Hello"
32
Variables
33
48> N = 1.149> N = 2.** exception error: no match of right hand side value 250> N = 1.1
34
Pattern matchingand Variables
35
2> {espresso, _, Crema} = Coffee.{espresso, "Harem", orange}
1> Coffee = harem:espresso().{espresso, "Harem", orange}
3> Crema.orange
36
Data Structures
37
[101, left, right, $a, [0, 1, 2], "abc"]
38
5> L = [101, left, right, "abc"]. [101,left,right,"abc"]
6> lists:nth(1,L). 101
39
7> lists:nthtail(1, L). [left,right,"abc"]
40
29> [66, 101 | "rlin"]."Berlin"
41
38> [First, Sec | Rest] = "Rest"."Rest"
39> First.82
40> [First]."R"41> Sec.10142> [Sec]. "e"
42
Strings sind Listen
43
Tuple
44
{1, 2, 3}
45
1> P = {adam,24,{july,29}}.{adam,24,{july,29}}
2> element(1,P).adam
46
3> element(3,P).{july,29}
47
4> P2 = setelement(2,P,25).{adam,25,{july,29}}
48
5> tuple_size(P).3
49
Binaries
50
<<13, 10, 13, 10>>
51
<<"GET / HTTP/1.1">>
52
web_request(Req) -> Req:ok({"text/html", <<"<html> <head><title> Hello Berlin </title></head> <body> Hello Berlin </body> </html>">>}).
53
54
0000000 d4 c3 b2 a1 02 00 04 00 00 00 00 00 00 00 00 000000010 60 00 00 00 01 00 00 00 07 aa f7 48 b4 32 08 000000020 4e 00 00 00 4e 00 00 00 00 14 6c 62 ef 18 00 190000030 e3 07 c0 bc 08 00 45 00 00 40 30 0d 40 00 40 060000040 3e 4e c0 a8 b2 cb d1 55 87 93 cd b9 00 50 a1 590000050 7f 98 00 00 00 00 b0 02 ff ff ad c0 00 00 02 040000060 05 b4 01 03 03 03 01 01 08 0a 27 c0 a6 25 00 000000070 00 00 04 02 00 00 0000076
55
<<Version:4, IHLen:4, TOService:8, TotalLength:16, Id:16, Flags:3, FragOffset:13, Ttl:8, Protocol:8, HeaderChecksum:16, SourceAddr:32, DestAddr:32>> = H.
56
43> A = 0.044> B = 8.845> C = 15.1546> Msg = <<A:9, B:12, C:16>>.<<0,0,64,0,15:5>>
57
dictarraysetssofs
digraph
58
Module file
59
-module(forest).-export([tree/1, tree/2]).
-include_lib("kernel/include/flora.hrl").
%% External API
tree(Age) -> {tree, Age}.tree(Age, Kind) -> {tree, Age, Kind}.
60
forest.erl
61
$ erlc forest.erl
62
forest.beam
63
Define Functions
64
double(X) -> 2 * X.
greet(Name) -> io:fwrite("Hallo ~p~n", [Name]),
ok.
65
1, 2, 3, 5, 8, 13, 21, 34, 55, 89
66
fib(0) -> 0;fib(1) -> 1;fib(N) when is_integer(N) -> fib(N-1) + fib(N-2).
67
For Want Of Loop
68
Recursion
69
vowels(Text) -> vowels(Text, []).
vowels([H|T], Acc) -> Acc1=case member(H, "aeiuo") of true -> [H | Acc]; false -> Acc end, vowels(T, Acc1);vowels([], Acc) -> reverse(Acc).
70
foo(N) when N > 1 -> ok.
71
bar(N) when is_list(N), length(N) ->
72
baz(I) when I > 0, I < 9 ->
73
quux(K) when I =< 0 or I >= 9 ->
74
double(N) when is_number(N) -> N * 2;double(N) when is_list(N) -> N ++ N.
75
process("/customer" ++ QString) -> customer(QString);
76
process("/customer" ++ QString) -> customer(QString);process("/employee" ++ QString) -> employee(QString);process(Any) -> ok.
77
Concurrent programming
78
Shared state
79
Message passing
80
81
spawn(Fun)
82
Pid = spawn(Fun)
83
Pid ! {lookup, {name, "Carl"}}
84
receive {lookup, Query} -> query(Query)end
85
receive {lookup, Query} -> query(Query); {update, Update} -> update(Update)end
86
receive Any -> Anyend
87
Pid ! Msg.receiver ! Msg.{receiver, '[email protected]'} ! Msg.
88
link(Pid)
89
process_flag(trap_exit, true)
90
{'EXIT', From, Reason}
91
1> self().<0.89.0>
92
Distributed Erlang
93
spawn(Node, Fun)
94
Node = 'serv@flomac'.
95
node()
96
nodes()
97
net_adm:ping(Node)
98
Function Functions
99
lists
100
mapfoldlfilter
101
lists:map(Fun, List)
102
L = [1,2,3,4,5],L1 = lists:map(fun(E) -> E * 2 end, L).[2,4,6,8,10]
103
lists:foldl(Fun, Acc, List)
104
L = [1,2,3,4,5],L1 = lists:foldl(fun(E, Acc) -> Acc + E end, L).
15
105
lists:filter(Fun, List)
106
L = [1,2,3,4,5],L1 = lists:filter(fun(E) -> E rem 2 == 0 end, L).[2,4]
107
108
rpc:pmap({Module, Function}, ExtraArgs, List2)
109
OTP
110
111
Supervision Tree Behaviours
Applications
Releases
Up- & Downgrades
112
113
{ch3, start_link, []}
114
init(_Args) -> {ok, {{one_for_one, 1, 60}, [{ch3, {ch3, start_link, []}, permanent, brutal_kill, worker, [ch3]}] }}.
115
116
Behaviour
117
Impl
Base Class
118
Callback
Behaviour
119
Library
120
kernel
rpc
erlang file
inetapplication io
error_logger
code
121
stdlib
filename
dictmath
stringsets
dets regexpproplists
arrayrandom
queue timer
shell
gen_server
lists
122
123
Do stuff
124
Unit Tests
125
succeed() -> ok.
fail() -> throw(failed).
succeeding_test() -> succeed().
failing_test() -> fail().
126
succeeding_assert_test_() -> ?_assert(1 > 0).
failing_assert_test_() -> ?_assert(0 > 1).
succeeding_error_test_() -> ?_assertError(foo, erlang:error(foo)).
failing_error_test_() -> ?_assertError(foo, erlang:throw(foo)).
127
File IO
128
{ok, Binary} = file:read_file(Filename)
129
{ok, Terms} = file:consult(Filename)
130
Macros
131
?MODULE?LINE
-define(OK, "200").?OK
-define(trace(A), ...)?trace
132
Hello world
133
$ sudo port install mochiweb$ escript /opt/local/share/scripts/ \ new_mochiweb.erl my \ app$ cd myapp ; make$ sh start-dev.sh
$ open localhost:8000
134
135
136
536 SeitenJuli '07
137
138
139
Record
141
-record(person, {name, age}).
142
.hrl
143
new(Name, Age) -> #person{name=Name, age=Age}.
144
name(P) -> P#person.name.
145
rename(P, NewName) -> P#person{name=Name}.
146
Where Next?
147
400 SeitenMärz '09
148
500 SeitenMai '09
149
portsedoc
debuggererlang-mode
disteltype specsdialyzer
mochiwebyaws
Lisp Flavoured ErlangReia
150
OTP Behaviours
151
gen_server
152
gen_fsm
153
supervisor
154
-module(sup).-behaviour(supervisor).
-export([start_link/0]).-export([init/1]).
start_link() -> supervisor:start_link(sup, []).
init(_Args) ->...
155
application
156
filename:dirname( code:which(?MODULE))
157
11> {ok,File} = file:open("PROJECTS", [read]).{ok,<0.92.0>}13> io:get_line(File, [])."Intro Erlang (am Mi., 17.10.08)\n"15> file:close(File).
158