Erlang Introduction Bcberlin3

4,509 views

Published on

Published in: Technology, News & Politics
1 Comment
9 Likes
Statistics
Notes
  • ...sound?
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
4,509
On SlideShare
0
From Embeds
0
Number of Embeds
23
Actions
Shares
0
Downloads
98
Comments
1
Likes
9
Embeds 0
No embeds

No notes for slide

Erlang Introduction Bcberlin3

  1. 1. Erlang Introduction 1
  2. 2. 2
  3. 3. 3
  4. 4. 4
  5. 5. 5
  6. 6. 6
  7. 7. 7
  8. 8. 8
  9. 9. Syntax 9
  10. 10. Hello User 10
  11. 11. erl 11
  12. 12. $ erl 1> | 12
  13. 13. 1> io:format(quot;Hello User~nquot;, []). Hello User ok 13
  14. 14. 2> 1 + 2008. 2009 14
  15. 15. 3> 1 + 2 + 3. 6 15
  16. 16. 10> 1 + 2 * 3. 7 11> 2 * 3 + 1. 7 12> (1 + 2) * 3. 9 16
  17. 17. 13> 3/0. ** exception error: bad argument in an arithmetic expression in operator '/'/2 called as 3 / 0 17
  18. 18. 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
  19. 19. 15> quot;Erlang introquot;. quot;Erlang introquot; 19
  20. 20. 35> quot;abcquot; ++ quot;defquot;. quot;abcdefquot; 20
  21. 21. 27> [72, 101, 108, 108, 111]. quot;Helloquot; 21
  22. 22. 27> [$E, $r, $l, $a, $n, $g]. quot;Erlangquot; 22
  23. 23. Atom 23
  24. 24. 6> atom. atom 24
  25. 25. ok true false kernel erlang 'EXIT' 'Name' 25
  26. 26. 6> is_atom(zbar). true 9> 1 == 3. false 26
  27. 27. Use Functions 27
  28. 28. 36> lists:reverse(quot;nilreB olleHquot;). quot;Hello Berlinquot; 28
  29. 29. 1> Mod = lists. lists 2> Fn = reverse. reverse 3> Mod:Fn(quot;tacquot;). quot;catquot; 29
  30. 30. 5> Mod:(list_to_atom(quot;reversequot;))(quot;atequot;). quot;etaquot; 30
  31. 31. 37> apply(lists, reverse, [quot;nilreBquot;]). quot;Berlinquot; 31
  32. 32. 39> Fn = fun lists:reverse/1. #Fun<lists.reverse.1> 40> apply(Fn, [quot;olleHquot;]). quot;Helloquot; 32
  33. 33. Variables 33
  34. 34. 48> N = 1. 1 49> N = 2. ** exception error: no match of right hand side value 2 50> N = 1. 1 34
  35. 35. Pattern matching and Variables 35
  36. 36. 1> Coffee = harem:espresso(). {espresso, quot;Haremquot;, orange} 2> {espresso, _, Crema} = Coffee. {espresso, quot;Haremquot;, orange} 3> Crema. orange 36
  37. 37. Data Structures 37
  38. 38. [101, left, right, $a, [0, 1, 2], quot;abcquot;] 38
  39. 39. 5> L = [101, left, right, quot;abcquot;]. [101,left,right,quot;abcquot;] 6> lists:nth(1,L). 101 39
  40. 40. 7> lists:nthtail(1, L). [left,right,quot;abcquot;] 40
  41. 41. 29> [66, 101 | quot;rlinquot;]. quot;Berlinquot; 41
  42. 42. 38> [First, Sec | Rest] = quot;Restquot;. quot;Restquot; 39> First. 82 40> [First]. quot;Rquot; 41> Sec. 101 42> [Sec]. quot;equot; 42
  43. 43. Strings sind Listen 43
  44. 44. Tuple 44
  45. 45. {1, 2, 3} 45
  46. 46. 1> P = {adam,24,{july,29}}. {adam,24,{july,29}} 2> element(1,P). adam 46
  47. 47. 3> element(3,P). {july,29} 47
  48. 48. 4> P2 = setelement(2,P,25). {adam,25,{july,29}} 48
  49. 49. 5> tuple_size(P). 3 49
  50. 50. Binaries 50
  51. 51. <<13, 10, 13, 10>> 51
  52. 52. <<quot;GET / HTTP/1.1quot;>> 52
  53. 53. web_request(Req) -> Req:ok({quot;text/htmlquot;, <<quot;<html> <head><title> Hello Berlin </title></head> <body> Hello Berlin </body> </html>quot;>>}). 53
  54. 54. 54
  55. 55. 0000000 d4 c3 b2 a1 02 00 04 00 00 00 00 00 00 00 00 00 0000010 60 00 00 00 01 00 00 00 07 aa f7 48 b4 32 08 00 0000020 4e 00 00 00 4e 00 00 00 00 14 6c 62 ef 18 00 19 0000030 e3 07 c0 bc 08 00 45 00 00 40 30 0d 40 00 40 06 0000040 3e 4e c0 a8 b2 cb d1 55 87 93 cd b9 00 50 a1 59 0000050 7f 98 00 00 00 00 b0 02 ff ff ad c0 00 00 02 04 0000060 05 b4 01 03 03 03 01 01 08 0a 27 c0 a6 25 00 00 0000070 00 00 04 02 00 00 0000076 55
  56. 56. <<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
  57. 57. 43> A = 0. 0 44> B = 8. 8 45> C = 15. 15 46> Msg = <<A:9, B:12, C:16>>. <<0,0,64,0,15:5>> 57
  58. 58. dict array sets sofs digraph 58
  59. 59. Module file 59
  60. 60. -module(forest). -export([tree/1, tree/2]). -include_lib(quot;kernel/include/ flora.hrlquot;). %% External API tree(Age) -> {tree, Age}. tree(Age, Kind) -> {tree, Age, Kind}. 60
  61. 61. forest.erl 61
  62. 62. $ erlc forest.erl 62
  63. 63. forest.beam 63
  64. 64. Define Functions 64
  65. 65. double(X) -> 2 * X. greet(Name) -> io:fwrite(quot;Hallo ~p~nquot;, [Name]), ok. 65
  66. 66. 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 66
  67. 67. fib(0) -> 0; fib(1) -> 1; fib(N) when is_integer(N) -> fib(N-1) + fib(N-2). 67
  68. 68. For Want Of Loop 68
  69. 69. Recursion 69
  70. 70. vowels(Text) -> vowels(Text, []). vowels([H|T], Acc) -> Acc1=case member(H, quot;aeiuoquot;) of true -> [H | Acc]; false -> Acc end, vowels(T, Acc1); vowels([], Acc) -> reverse(Acc). 70
  71. 71. foo(N) when N > 1 -> ok. 71
  72. 72. bar(N) when is_list(N), length(N) -> 72
  73. 73. baz(I) when I > 0, I < 9 -> 73
  74. 74. quux(K) when I =< 0 or I >= 9 -> 74
  75. 75. double(N) when is_number(N) -> N * 2; double(N) when is_list(N) -> N ++ N. 75
  76. 76. process(quot;/customerquot; ++ QString) -> customer(QString); 76
  77. 77. process(quot;/customerquot; ++ QString) -> customer(QString); process(quot;/employeequot; ++ QString) -> employee(QString); process(Any) -> ok. 77
  78. 78. Concurrent programming 78
  79. 79. Shared state 79
  80. 80. Message passing 80
  81. 81. 81
  82. 82. spawn(Fun) 82
  83. 83. Pid = spawn(Fun) 83
  84. 84. Pid ! {lookup, {name, quot;Carlquot;}} 84
  85. 85. receive {lookup, Query} -> query(Query) end 85
  86. 86. receive {lookup, Query} -> query(Query); {update, Update} -> update(Update) end 86
  87. 87. receive Any -> Any end 87
  88. 88. Pid ! Msg. receiver ! Msg. {receiver, 'notos@nugg.ad'} ! Msg. 88
  89. 89. link(Pid) 89
  90. 90. process_flag(trap_exit, true) 90
  91. 91. {'EXIT', From, Reason} 91
  92. 92. 1> self(). <0.89.0> 92
  93. 93. Distributed Erlang 93
  94. 94. spawn(Node, Fun) 94
  95. 95. Node = 'serv@flomac'. 95
  96. 96. node() 96
  97. 97. nodes() 97
  98. 98. net_adm:ping(Node) 98
  99. 99. Function Functions 99
  100. 100. lists 100
  101. 101. map foldl filter 101
  102. 102. lists:map(Fun, List) 102
  103. 103. L = [1,2,3,4,5], L1 = lists:map(fun(E) -> E * 2 end, L). [2,4,6,8,10] 103
  104. 104. lists:foldl(Fun, Acc, List) 104
  105. 105. L = [1,2,3,4,5], L1 = lists:foldl(fun(E, Acc) -> Acc + E end, L). 15 105
  106. 106. lists:filter(Fun, List) 106
  107. 107. L = [1,2,3,4,5], L1 = lists:filter(fun(E) -> E rem 2 == 0 end, L). [2,4] 107
  108. 108. 108
  109. 109. rpc:pmap({Module, Function}, ExtraArgs, List2) 109
  110. 110. OTP 110
  111. 111. 111
  112. 112. Supervision Tree Behaviours Applications Releases Up- & Downgrades 112
  113. 113. 113
  114. 114. {ch3, start_link, []} 114
  115. 115. init(_Args) -> {ok, {{one_for_one, 1, 60}, [{ch3, {ch3, start_link, []}, permanent, brutal_kill, worker, [ch3]}] }}. 115
  116. 116. 116
  117. 117. Behaviour 117
  118. 118. Base Class Impl 118
  119. 119. Behaviour Callback 119
  120. 120. Library 120
  121. 121. kernel erlang code file rpc application io inet error_logger 121
  122. 122. stdlib math dict filename sets string lists proplists dets regexp random shell array queue timer gen_server 122
  123. 123. 123
  124. 124. Do stuff 124
  125. 125. Unit Tests 125
  126. 126. succeed() -> ok. fail() -> throw(failed). succeeding_test() -> succeed(). failing_test() -> fail(). 126
  127. 127. 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
  128. 128. File IO 128
  129. 129. {ok, Binary} = file:read_file(Filename) 129
  130. 130. {ok, Terms} = file:consult(Filename) 130
  131. 131. Macros 131
  132. 132. ?MODULE ?LINE -define(OK, quot;200quot;). ?OK -define(trace(A), ...) ?trace 132
  133. 133. Hello world 133
  134. 134. $ 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. 135. 135
  136. 136. 136
  137. 137. 536 Seiten Juli '07 137
  138. 138. 138
  139. 139. 139
  140. 140. C. Florian Ebeling florian.ebeling@gmail.com florian.ebeling@nugg.ad @febeling 140
  141. 141. Record 141
  142. 142. -record(person, {name, age}). 142
  143. 143. .hrl 143
  144. 144. new(Name, Age) -> #person{name=Name, age=Age}. 144
  145. 145. name(P) -> P#person.name. 145
  146. 146. rename(P, NewName) -> P#person{name=Name}. 146
  147. 147. Where Next? 147
  148. 148. 400 Seiten März '09 148
  149. 149. 500 Seiten Mai '09 149
  150. 150. ports edoc debugger erlang-mode distel type specs dialyzer mochiweb yaws Lisp Flavoured Erlang Reia 150
  151. 151. OTP Behaviours 151
  152. 152. gen_server 152
  153. 153. gen_fsm 153
  154. 154. supervisor 154
  155. 155. -module(sup). -behaviour(supervisor). -export([start_link/0]). -export([init/1]). start_link() -> supervisor:start_link(sup, []). init(_Args) -> ... 155
  156. 156. application 156
  157. 157. filename:dirname( code:which(?MODULE)) 157
  158. 158. 11> {ok,File} = file:open(quot;PROJECTSquot;, [read]). {ok,<0.92.0>} 13> io:get_line(File, []). quot;Intro Erlang (am Mi., 17.10.08)nquot; 15> file:close(File). 158

×