-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherlang.html
More file actions
10920 lines (8283 loc) · 435 KB
/
Copy patherlang.html
File metadata and controls
10920 lines (8283 loc) · 435 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html><html><body><title>Erlang Tutorial</title>
<h1>Erlang Tutorial</h1>
<p>Erlang is a general purpose or you might say a functional programming language and runtime environment. It was built in such a way that it had inherent support for concurrency, distribution and fault tolerance. Erlang was originally developed to be used in several large telecommunication systems. But it has now slowly made its foray into diverse sectors like ecommerce, computer telephony and banking sectors as well.</p>
<h1>Audience</h1>
<p>This tutorial has been prepared for professionals aspiring to make a career in the field of telecom, banking, instant messaging, e-commerce and computer telephony as well. This tutorial will give you enough understanding on this programming language and also help you in building scalable soft real time systems that will have requirements on higher availability.</p>
<h1>Prerequisites</h1>
<p>Before proceeding with this tutorial, you must have some basic knowledge on programming in the following languages such as C or C++, Java, Python, Ruby. Furthermore, it might also be helpful, to have some working knowledge on functional programming languages like Clojure, Haskell, Scala or OCaml for advanced programming on Erlang.</p>
<h1>Execute Erlang Online</h1>
<p>For most of the examples given in this tutorial you will find Try it option, so just make use of this option to execute your Erlang programs at the spot and enjoy your learning.</p>
<p>Try following example using Try it option available at the top right corner of the below sample code box −</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="prettyprint notranslate tryit">
% hello world program
-module(helloworld).
-export([start/0]).
start() ->
io:fwrite("Hello, world!\n").
</pre>
<title>Erlang Overview</title>
<h1>Erlang - Overview</h1>
<p>Erlang is a functional programming language which also has a runtime environment. It was built in such a way that it had integrated support for concurrency, distribution and fault tolerance. Erlang was originally developed to be used in several large telecommunication systems from Ericsson.</p>
<p>The first version of Erlang was developed by Joe Armstrong, Robert Virding and Mike Williams in 1986. It was originally a proprietary language within Ericsson. It was later released as an open source language in year 1998. Erlang, along with OTP, a collection of middleware and libraries in Erlang, are now supported and maintained by the OTP product unit at Ericsson and widely referred to as <b>Erlang/OTP</b>.</p>
<h2>Why Erlang?</h2>
<p>Erlang should be used to develop your application, if you have the following requirements −</p>
<p>The application needs to handle a large number of concurrent activities.</p>
<p>It should be easily distributable over a network of computers.</p>
<p>There should be a facility to make the application fault-tolerant to both software and hardware errors.</p>
<p>The application should be scalable. This means that it should have the ability to span across multiple servers with little or no change.</p>
<p>It should be easily upgradable and reconfigurable without having to stop and restart the application itself.</p>
<p>The application should be responsive to users within certain strict timeframes.</p>
<p>The official website for Erlang is <a target="_blank" rel="nofollow" href="http://www.erlang.org/">http://www.erlang.org/</a></p>
<p>© Copyright 2017. All Rights Reserved.</p>
<title>Erlang Environment</title>
<h1>Erlang - Environment</h1>
<h2>Try it Option Online</h2>
<p>You really do not need to set up your own environment to start learning Erlang programming language. Reason is very simple, we have already set up Erlang Programming environment online, so that you can compile and execute all the available examples online at the same time when you are doing your theory work. This gives you confidence in what you are reading and to check the result with different options. Feel free to modify any example and execute it online.</p>
<p>Try the following example using our online compiler option available at <a rel="nofollow" target="_blank" href="http://www.tutorialspoint.com/codingground.htm">CodingGround</a></p>
<p>For most of the examples given in this tutorial, you will find <b>Try it</b> option in our website code sections at the top right corner that will take you to the online compiler. So just make use of it and enjoy your learning.</p>
<p>Now before you can start working on Erlang, you need to ensure that you have a fully functional version of Erlang running on your system. This section will look into the installation of Erlang and its subsequent configuration on a windows machine to get started with Erlang.</p>
<p>Ensure that the following system requirements are met before proceeding with the installation.</p>
<p><b>System Requirements</b></p>
<h2>Downloading Erlang</h2>
<p>To download Erlang, one must go to the following url − <a target="_blank" rel="nofollow" href="http://www.erlang.org/downloads">http://www.erlang.org/downloads</a></p>
<p>This page has a variety of downloads and also the steps required to download and install the language on Linux and Mac platforms.</p>
<p>Click on the ‘OTP 18.3 Windows 32-bit Binary File’ to begin the download of the Erlang Windows Installation file.</p>
<h2>Erlang Installation</h2>
<p>The following steps detail how Erlang can be installed on Windows −</p>
<p><b>Step 1</b> − Launch the Installer downloaded in the earlier section. After the installer starts, click Run.</p>
<p><b>Step 2</b> − Click Next on the following screen to accept the default components, which will be installed.</p>
<p><b>Step 3</b> − Accept the default installation path and click Next.</p>
<p><b>Step 4</b> − Accept the default Start Menu item, which will be created and click Next.</p>
<p><b>Step 5</b> − After the installation is complete, click Close to complete the installation.</p>
<h2>Erlang Configuration</h2>
<p>After the installation is complete the following configuration needs to be carried out to ensure that Erlang starts working on the system.</p>
<p>If you now open the command prompt and type <b>erl</b>, you should be able to get the erl command prompt.</p>
<p>Congratulations, you now have erl successfully configured on your laptop.</p>
<h2>Installation of Plugin-ins on Popular IDE’s</h2>
<p>Erlang as a programming language is also available in popular IDE’s such as <b>Eclipse and IntelliJ</b>. Let’s look at how we can get the required plugin’s in these IDE’s so that you have more choices in working with Erlang.</p>
<h3>Installation in Eclipse</h3>
<p><b>Step 1</b> − Open Eclipse and click the Menu item, <b>Help → Install New Software</b>.</p>
<p><b>Step 2</b> − Enter the Work with link as <a target="_blank" rel="nofollow" href="http://download.erlide.org/update/">http://download.erlide.org/update</a></p>
<p>Then click Add.</p>
<p><b>Step 3</b> − You will then be prompted to enter a Name for the plugin, enter the name as <b>Erlide</b>. Click Ok.</p>
<p><b>Step 4</b> − Eclipse will then scan the link provided and get the required plugins. Check the plugins and click Next.</p>
<p><b>Step 5</b> − In the next dialog box, Eclipse will show all the components which will be installed. Click Next.</p>
<p><b>Step 6</b> − In the next dialog box, Eclipse will just ask to review the components being installed. Click Next.</p>
<p><b>Step 7</b> − In the next dialog box, you just need to accept the license agreement. Finally, click the Finish button.</p>
<p>The installation will then begin, and once completed, it will prompt you to restart Eclipse.</p>
<p>Once Eclipse is restarted, when you create a project, you will be able to see Erlang as an option as well.</p>
<h3>Installation in IntelliJ</h3>
<p>Please follow the subsequent steps to install IntelliJ in your computer.</p>
<p><b>Step 1</b> − Open IntelliJ and click Configure → Plugins.</p>
<p><b>Step 2</b> − Type Erlang in the search box. You will get Erlang plugin on the right hand side of the screen. Click the Install button.</p>
<p><b>Step 3</b> − After the Erlang plugin is installed, you will be prompted to restart the IDE.</p>
<p>When you restart the IDE and try to create a new project, you will see the option to create an Erlang project.</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="prettyprint notranslate tryit">
% hello world program
-module(helloworld).
-export([start/0]).
start() ->
io:fwrite("Hello, world!\n").
</pre>
<title>Erlang Basic Syntax</title>
<h1>Erlang - Basic Syntax</h1>
<p>In order to understand the basic syntax of Erlang, let’s first look at a simple <b>Hello World</b> program.</p>
<h3>Example</h3>
<p>The following things need to be noted about the above program −</p>
<p>The % sign is used to add comments to the program.</p>
<p>The module statement is like adding a namespace as in any programming language. So over here, we are mentioning that this code will part of a module called <b>helloworld</b>.</p>
<p>The export function is used so that any function defined within the program can be used. We are defining a function called start and in order to use the start function, we have to use the export statement. The <b>/0</b> means that our function ‘start’ accepts 0 parameters.</p>
<p>We finally define our start function. Here we use another module called <b>io</b> which has all the required Input Output functions in Erlang. We used the <b>fwrite</b> function to output “Hello World” to the console.</p>
<p>The output of the above program will be −</p>
<h3>Output</h3>
<h2>General Form of a Statement</h2>
<p>In Erlang, you have seen that there are different symbols used in Erlang language. Let’s go through what we have seen from a simple Hello World program −</p>
<p>The hyphen symbol <b>(–)</b> is generally used along with the module, import and export statement. The hyphen symbol is used to give meaning to each statement accordingly. So examples from the Hello world program are shown in the following program −</p>
<p>Each statement is delimited with the dot <b>(.)</b> symbol. Each statement in Erlang needs to end with this delimiter. An example from the Hello world program is as shown in the following program −</p>
<p>The slash <b>(/)</b> symbol is used along with the function to define the number of parameters which is accepted by the function.</p>
<h2>Modules</h2>
<p>In Erlang, all the code is divided into modules. A module consists of a sequence of attributes and function declarations. It is just like a concept of a namespace in other programming languages which is used to logically separate different units of code.</p>
<h3>Defining a module</h3>
<p>A module is defined with the module identifier. The general syntax and example is as follows.</p>
<h3>Syntax</h3>
<p>The <b>ModuleName</b> needs to be same as the file name minus the extension <b>.erl</b>. Otherwise code loading will not work as intended.</p>
<h3>Example</h3>
<p>These Modules will be covered in detail in the ensuing chapters, this was just to get you at a basic understanding of how a module should be defined.</p>
<h2>Import Statement in Erlang</h2>
<p>In Erlang, if one wants to use the functionality of an existing Erlang module, one can use the import statement. The general form of the import statement is depicted in the following program −</p>
<h3>Example</h3>
<p>Where,</p>
<p><b>Modulename</b> − This is the name of the module which needs to be imported.</p>
<p><b>functionname/parameter</b> − The function in the module which needs to be imported.</p>
<p>Let’s change the way we write our hello world program to use an import statement. The example would be as shown in the following program.</p>
<h3>Example</h3>
<p>In the above code, we are using the import keyword to import the library ‘io’ and specifically the <b>fwrite</b> function. So now whenever we invoke the fwrite function, we don’t have to mention the <b>io</b> module name everywhere.</p>
<h2>Keywords in Erlang</h2>
<p>A Keyword is a reserved word in Erlang which should not be used for any different purposes other than the purpose which it has been intended for. Following are the list of keywords in Erlang.</p>
<h2>Comments in Erlang</h2>
<p>Comments are used to document your code. Single line comments are identified by using the <b>%</b> symbol at any position in the line. Following is an example for the same −</p>
<h3>Example</h3>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="prettyprint notranslate tryit">
% hello world program
-module(helloworld).
-export([start/0]).
start() ->
io:fwrite("Hello, world!\n").
</pre>
<p>The following things need to be noted about the above program −</p>
<ul class="list">
<li><p>The % sign is used to add comments to the program.</p></li>
<li><p>The module statement is like adding a namespace as in any programming language. So over here, we are mentioning that this code will part of a module called <b>helloworld</b>.</p></li>
<li><p>The export function is used so that any function defined within the program can be used. We are defining a function called start and in order to use the start function, we have to use the export statement. The <b>/0</b> means that our function ‘start’ accepts 0 parameters.</p></li>
<li><p>We finally define our start function. Here we use another module called <b>io</b> which has all the required Input Output functions in Erlang. We used the <b>fwrite</b> function to output “Hello World” to the console.</p></li>
</ul>
<p>The output of the above program will be −</p>
<h3>Output</h3>
<pre class="result notranslate">
Hello, world!
</pre>
<h2>General Form of a Statement</h2>
<p>In Erlang, you have seen that there are different symbols used in Erlang language. Let’s go through what we have seen from a simple Hello World program −</p>
<ul class="list">
<li><p>The hyphen symbol <b>(–)</b> is generally used along with the module, import and export statement. The hyphen symbol is used to give meaning to each statement accordingly. So examples from the Hello world program are shown in the following program −</p></li>
</ul>
<pre class="result notranslate">
-module(helloworld).
-export([start/0]).
</pre>
<p>Each statement is delimited with the dot <b>(.)</b> symbol. Each statement in Erlang needs to end with this delimiter. An example from the Hello world program is as shown in the following program −</p>
<pre class="result notranslate">
io:fwrite("Hello, world!\n").
</pre>
<ul class="list">
<li><p>The slash <b>(/)</b> symbol is used along with the function to define the number of parameters which is accepted by the function.</p></li>
</ul>
<pre class="result notranslate">
-export([start/0]).
</pre>
<h2>Modules</h2>
<p>In Erlang, all the code is divided into modules. A module consists of a sequence of attributes and function declarations. It is just like a concept of a namespace in other programming languages which is used to logically separate different units of code.</p>
<h3>Defining a module</h3>
<p>A module is defined with the module identifier. The general syntax and example is as follows.</p>
<h3>Syntax</h3>
<pre class="result notranslate">
-module(ModuleName)
</pre>
<p>The <b>ModuleName</b> needs to be same as the file name minus the extension <b>.erl</b>. Otherwise code loading will not work as intended.</p>
<h3>Example</h3>
<pre class="result notranslate">
-module(helloworld)
</pre>
<p>These Modules will be covered in detail in the ensuing chapters, this was just to get you at a basic understanding of how a module should be defined.</p>
<h2>Import Statement in Erlang</h2>
<p>In Erlang, if one wants to use the functionality of an existing Erlang module, one can use the import statement. The general form of the import statement is depicted in the following program −</p>
<h3>Example</h3>
<pre class="result notranslate">
-import (modulename, [functionname/parameter]).
</pre>
<p>Where,</p>
<ul class="list">
<li><p><b>Modulename</b> − This is the name of the module which needs to be imported.</p></li>
<li><p><b>functionname/parameter</b> − The function in the module which needs to be imported.</p></li>
</ul>
<p>Let’s change the way we write our hello world program to use an import statement. The example would be as shown in the following program.</p>
<h3>Example</h3>
<pre class="prettyprint notranslate">
% hello world program
-module(helloworld).
-import(io,[fwrite/1]).
-export([start/0]).
start() ->
fwrite("Hello, world!\n").
</pre>
<p>In the above code, we are using the import keyword to import the library ‘io’ and specifically the <b>fwrite</b> function. So now whenever we invoke the fwrite function, we don’t have to mention the <b>io</b> module name everywhere.</p>
<h2>Keywords in Erlang</h2>
<p>A Keyword is a reserved word in Erlang which should not be used for any different purposes other than the purpose which it has been intended for. Following are the list of keywords in Erlang.</p>
<table style="text-align:center;" class="table table-bordered">
<tr>
<td>after</td>
<td>and</td>
<td>andalso</td>
<td>band</td>
</tr>
<tr>
<td>begin</td>
<td>bnot</td>
<td>bor</td>
<td>bsl</td>
</tr>
<tr>
<td>bsr</td>
<td>bxor</td>
<td>case</td>
<td>catch</td>
</tr>
<tr>
<td>cond</td>
<td>div</td>
<td>end</td>
<td>fun</td>
</tr>
<tr>
<td>if</td>
<td>let</td>
<td>not</td>
<td>of</td>
</tr>
<tr>
<td>or</td>
<td>orelse</td>
<td>receive</td>
<td>rem</td>
</tr>
<tr>
<td>try</td>
<td>when</td>
<td>xor</td>
<td></td>
</tr>
</table>
<h2>Comments in Erlang</h2>
<p>Comments are used to document your code. Single line comments are identified by using the <b>%</b> symbol at any position in the line. Following is an example for the same −</p>
<h3>Example</h3>
<pre class="prettyprint notranslate">
% hello world program
-module(helloworld).
% import function used to import the io module
-import(io,[fwrite/1]).
% export function used to ensure the start function can be accessed.
-export([start/0]).
start() ->
fwrite("Hello, world!\n").
</pre>
<title>Erlang Shell</title>
<h1>Erlang - Shell</h1>
<p>The Erlang shell is used for testing of expressions. Hence, testing can be carried out in the shell very easily before it actually gets tested in the application itself.</p>
<p>The following example showcases how the addition expression can be used in the shell. What needs to be noted here is that the expression needs to end with the dot (.) delimiter.</p>
<p>After the command is executed, the shell prints out another prompt, this time for Command Number 2 (because the command number increases each time a new command is entered).</p>
<p>The following functions are the most common one’s used in the Erlang shell.</p>
<p><b>b()</b> − Prints the current variable bindings.</p>
<p><b>Syntax</b> − b().</p>
<p><b>For example</b> − Following is an example of how the function is used. First a variable called <b>Str</b> is defined, which has the value <b>abcd</b>. Then <b>b()</b> is used to display all the binded variables.</p>
<p><b>f()</b> − Removes all current variable bindings.</p>
<p><b>Syntax</b> − f().</p>
<p><b>For example</b> − Following is an example of how the function is used. First a variable called Str is defined which has the value abcd. The f() is then used to remove the Str variable binding. The b() is then called to ensure the binding has been successfully removed.</p>
<p><b>f(x)</b> − Removes the binding for a particular variable.</p>
<p><b>Syntax</b> − f(x). Where, x – is the variable for which the binding needs to be removed.</p>
<p><b>For example</b> − Following is an example of how the function is used. First a variable called Str and Str1 are defined. The f(Str) is then used to remove the Str variable binding. The b() is then called to ensure the binding has been successfully removed.</p>
<p><b>h()</b> − Prints the history list of all the commands executed in the shell.</p>
<p><b>Syntax</b> − h().</p>
<p><b>For example</b> − An example of the h() command, which prints the history of commands executed in the shell is shown in the following screenshot.</p>
<p><b>history(N)</b> − Sets the number of previous commands to keep in the history list to N. The previous number is returned. The default number is 20.</p>
<p><b>Syntax</b> − history(N). Where, N – is the number to which the command history list needs to be limited to.</p>
<p><b>For example</b> − An example of the history(N) command is shown in the following screenshot.</p>
<p><b>e(N)</b> − Repeats the command N, if N is positive. If it is negative, the Nth previous command is repeated (i.e., e(-1) repeats the previous command).</p>
<p><b>Syntax</b> − e(N). Where, N –is the command at the Nth position in the list.</p>
<p><b>For example</b> − An example of the e(N) command is shown below. Since we have executed the e(-1) command, it will execute the previous command which was history(5).</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<title>Erlang Data Types</title>
<h1>Erlang - Data Types</h1>
<p>In any programming language, you need to use several variables to store various types of information. Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in the memory to store the value associated with that variable.</p>
<p>You may like to store information of various data types like string, character, wide character, integer, floating point, Boolean, etc. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory.</p>
<h2>Built-in Data Types</h2>
<p>Erlang offers a wide variety of built-in data types. Following is a list of data types which are defined in Erlang −</p>
<p><b>Number</b> − In Erlang, there are 2 types of numeric literals which are integers and floats.</p>
<p><b>Atom</b> − An atom is a literal, a constant with name. An atom is to be enclosed in single quotes (') if it does not begin with a lower-case letter or if it contains other characters than alphanumeric characters, underscore (_), or @.</p>
<p><b>Boolean</b> − Boolean data types in Erlang are the two reserved atoms: true and false.</p>
<p><b>Bit String</b> − A bit string is used to store an area of un-typed memory.</p>
<p><b>Tuple</b> − A tuple is a compound data type with a fixed number of terms. Each Term in the tuple is called as an element. The number of elements is said to be the size of the tuple.</p>
<p><b>Map</b> − A map is a compound data type with a variable number of key-value associations. Each key-value association in the map is called an association pair. The key and value parts of the pair are called elements. The number of association pairs is said to be the size of the map.</p>
<p><b>List</b> − A list is a compound data type with a variable number of terms. Each term in the list is called an element. The number of elements is said to be the length of the list.</p>
<p><b>Note</b> − You will be surprised to see that you cannot see the String type anywhere in the list above. That’s because there is no string data type exclusively defined in Erlang. But we will see how we can work with strings in a subsequent chapter.</p>
<p>Following are the examples of how each data type can be used. Again each data type will be discussed in detail in the ensuing chapters. This is just to get you acquainted with a brief description of the above-mentioned data types.</p>
<h3>Number</h3>
<p>An example of how the number data type can be used is shown in the following program. This program shows the addition of 2 Integers.</p>
<p><b>Example</b></p>
<p>The output of the above program will be −</p>
<p><b>Output</b></p>
<h3>Atom</h3>
<p>Atoms should begin with a lowercase letter and can contain lowercase and uppercase characters, digits, underscore <b>(_)</b> and the “at” sign <b>(@)</b>. We can also enclose an atom in single quotes.</p>
<p>An example of how the atom data type can be used is shown in the following program. In this program, we are creating an atom which is called atom1.</p>
<p><b>Example</b></p>
<p>The output of the above program will be −</p>
<p><b>Output</b></p>
<h3>Boolean</h3>
<p>An example of how the Boolean data type can be used is shown in the following program. This example does a comparison between 2 integers and prints the resultant Boolean to the console.</p>
<p><b>Example</b></p>
<p>The output of the above program will be −</p>
<p><b>Output</b></p>
<h3>Bit String</h3>
<p>An example of how the Bit String data type can be used is shown in the following program. This program defines a Bit String consisting of 2 bits. The <b>binary_to_list</b> is an inbuilt function defined in Erlang which can be used to convert a Bit String to a list.</p>
<p><b>Example</b></p>
<p>The output of the above program will be −</p>
<p><b>Output</b></p>
<h3>Tuple</h3>
<p>An example of how the Tuple data type can be used is shown in the following program.</p>
<p>Here we are defining a <b>Tuple P</b> which has 3 terms. The <b>tuple_size</b> is an inbuilt function defined in Erlang, which can be used to determine the size of the tuple.</p>
<p><b>Example</b></p>
<p>The output of the above program will be −</p>
<p><b>Output</b></p>
<h3>Map</h3>
<p>An example of how the Map data type can be used is shown in the following program.</p>
<p>Here we are defining a <b>Map M1</b> which has 2 mappings. The <b>map_size</b> is an inbuilt function defined in Erlang, which can be used to determine the size of the map.</p>
<p><b>Example</b></p>
<p>The output of the above program will be −</p>
<p><b>Output</b></p>
<h3>List</h3>
<p>An example of how the List data type can be used is shown in the following program.</p>
<p>Here we are defining a <b>List L</b> which has 3 items. The length is an inbuilt function defined in Erlang, which can be used to determine the size of the list.</p>
<p><b>Example</b></p>
<p>The output of the above program will be −</p>
<p><b>Output</b></p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="prettyprint notranslate tryit">
-module(helloworld).
-export([start/0]).
start() ->
io:fwrite("~w",[1+1]).
</pre>
<p>The output of the above program will be −</p>
<p><b>Output</b></p>
<pre class="result notranslate">
2
</pre>
<h3>Atom</h3>
<p>Atoms should begin with a lowercase letter and can contain lowercase and uppercase characters, digits, underscore <b>(_)</b> and the “at” sign <b>(@)</b>. We can also enclose an atom in single quotes.</p>
<p>An example of how the atom data type can be used is shown in the following program. In this program, we are creating an atom which is called atom1.</p>
<p><b>Example</b></p>
<pre class="prettyprint notranslate tryit">
-module(helloworld).
-export([start/0]).
start() ->
io:fwrite(atom1).
</pre>
<p>The output of the above program will be −</p>
<p><b>Output</b></p>
<pre class="result notranslate">
atom1
</pre>
<h3>Boolean</h3>
<p>An example of how the Boolean data type can be used is shown in the following program. This example does a comparison between 2 integers and prints the resultant Boolean to the console.</p>
<p><b>Example</b></p>
<pre class="prettyprint notranslate tryit">
-module(helloworld).
-export([start/0]).
start() ->
io:fwrite(2 =< 3).
</pre>
<p>The output of the above program will be −</p>
<p><b>Output</b></p>
<pre class="result notranslate">
true
</pre>
<h3>Bit String</h3>
<p>An example of how the Bit String data type can be used is shown in the following program. This program defines a Bit String consisting of 2 bits. The <b>binary_to_list</b> is an inbuilt function defined in Erlang which can be used to convert a Bit String to a list.</p>
<p><b>Example</b></p>
<pre class="prettyprint notranslate tryit">
-module(helloworld).
-export([start/0]).
start() ->
Bin1 = <<10,20>>,
X = binary_to_list(Bin1),
io:fwrite("~w",[X]).
</pre>
<p>The output of the above program will be −</p>
<p><b>Output</b></p>
<pre class="result notranslate">
[10,20]
</pre>
<h3>Tuple</h3>
<p>An example of how the Tuple data type can be used is shown in the following program.</p>
<p>Here we are defining a <b>Tuple P</b> which has 3 terms. The <b>tuple_size</b> is an inbuilt function defined in Erlang, which can be used to determine the size of the tuple.</p>
<p><b>Example</b></p>
<pre class="prettyprint notranslate tryit">
-module(helloworld).
-export([start/0]).
start() ->
P = {john,24,{june,25}} ,
io:fwrite("~w",[tuple_size(P)]).
</pre>
<p>The output of the above program will be −</p>
<p><b>Output</b></p>
<pre class="result notranslate">
3
</pre>
<h3>Map</h3>
<p>An example of how the Map data type can be used is shown in the following program.</p>
<p>Here we are defining a <b>Map M1</b> which has 2 mappings. The <b>map_size</b> is an inbuilt function defined in Erlang, which can be used to determine the size of the map.</p>
<p><b>Example</b></p>
<pre class="prettyprint notranslate tryit">
-module(helloworld).
-export([start/0]).
start() ->
M1 = #{name=>john,age=>25},
io:fwrite("~w",[map_size(M1)]).
</pre>
<p>The output of the above program will be −</p>
<p><b>Output</b></p>
<pre class="result notranslate">
2
</pre>
<h3>List</h3>
<p>An example of how the List data type can be used is shown in the following program.</p>
<p>Here we are defining a <b>List L</b> which has 3 items. The length is an inbuilt function defined in Erlang, which can be used to determine the size of the list.</p>
<p><b>Example</b></p>
<pre class="prettyprint notranslate tryit">
-module(helloworld).
-export([start/0]).
start() ->
L = [10,20,30] ,
io:fwrite("~w",[length(L)]).
</pre>
<p>The output of the above program will be −</p>
<p><b>Output</b></p>
<pre class="result notranslate">
3
</pre>
<title>Erlang Variables</title>
<h1>Erlang - Variables</h1>
<p>In Erlang, all the variables are bound with the ‘=’ statement. All variables need to start with the upper case character. In other programming languages, the ‘=’ sign is used for the assignment, but not in the case of Erlang. As stated, variables are defined with the use of the ‘=’ statement.</p>
<p>One key thing to note in Erlang is that variables are immutable, which means that in order for the value of the variable to change, it needs to be destroyed and recreated again.</p>
<p>The following basic variables in Erlang are explained in the last chapter −</p>
<p><b>Numbers</b> − This is used to represent an integer or a float. An example is 10.</p>
<p><b>Boolean</b> − This represents a Boolean value which can either be true or false.</p>
<p><b>Bit String</b> − A bit string is used to store an area of un-typed memory. An example is <<40,50>>.</p>
<p><b>Tuple</b> − A tuple is a compound data type with a fixed number of terms. An example is {40,50}.</p>
<p><b>Map</b> − A map is a compound data type with a variable number of key-value associations. Each key-value association in the map is called an association pair. An example is {type=>person,age=>25}.</p>
<p><b>List</b> − A list is a compound data type with a variable number of terms. An example is [40,40].</p>
<h2>Variable Declarations</h2>
<p>The general syntax of defining a variable is as follows −</p>
<h3>Syntax</h3>
<p>Where,</p>
<p><b>var-name</b> − This is the name of the variable.</p>
<p><b>var-value</b> − This is the value bound to the variable.</p>
<p>Following is an example of variable declaration −</p>
<h3>Example</h3>
<p>In the above example, we have 2 variables, one is X which is bound to the value 40 and the next is Y which is bound to the value of 50. Another variable called Result is bound to the addition of X and Y.</p>
<p>The output of the above program will be −</p>
<h3>Output</h3>
<h2>Naming Variables</h2>
<p>As discussed, variable names have to start with uppercase. Let’s take an example of a variable declared in lower case.</p>
<h3>Example</h3>
<p>If you try to compile the above program, you will get the following compile time error.</p>
<h3>Output</h3>
<p>Secondly, all variables can only be assigned once. Let’s take an example of assigning a variable more than once.</p>
<h3>Example</h3>
<p>If you try to compile the above program, you will receive the following compile time error.</p>
<h3>Output</h3>
<h2>Printing Variables</h2>
<p>In this section we will discuss how to use the various functions of printing variables.</p>
<h3>Using the io:fwrite function</h3>
<p>You would have seen this (io:fwrite) used in all of the above programs. The <b>fwrite</b> function is part of the ‘io’ module or Erlang, which can be used to output the value of variables in the program.</p>
<p>The following example shows a few more parameters which can be used with the fwrite statement.</p>
<h3>Example</h3>
<p>The output of the above program will be −</p>
<h3>Output</h3>
<p>The following pointers should be noted about the above program.</p>
<p><b>~</b> − This character symbolizes that some formatting needs to be carried out for the output.</p>
<p><b>~f</b> − The argument is a float which is written as [-]ddd.ddd, where the precision is the number of digits after the decimal point. The default precision is 6 and it cannot be less than 1.</p>
<p><b>~n</b> − This is to <b>println</b> to a new line.</p>
<p><b>~e</b> − The argument is a float which is written as [-]d.ddde+-ddd, where the precision is the number of digits written. The default precision is 6 and it cannot be less than 2.</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="result notranslate">
var-name = var-value
</pre>
<p>Where,</p>
<ul class="list">
<li><p><b>var-name</b> − This is the name of the variable.</p></li>
<li><p><b>var-value</b> − This is the value bound to the variable.</p></li>
</ul>
<p>Following is an example of variable declaration −</p>
<h3>Example</h3>
<pre class="prettyprint notraslate tryit">
-module(helloworld).
-export([start/0]).
start() ->
X = 40,
Y = 50,
Result = X + Y,
io:fwrite("~w",[Result]).
</pre>
<p>In the above example, we have 2 variables, one is X which is bound to the value 40 and the next is Y which is bound to the value of 50. Another variable called Result is bound to the addition of X and Y.</p>
<p>The output of the above program will be −</p>
<h3>Output</h3>
<pre class="result notraslate">
90
</pre>
<h2>Naming Variables</h2>
<p>As discussed, variable names have to start with uppercase. Let’s take an example of a variable declared in lower case.</p>
<h3>Example</h3>
<pre class="prettyprint notraslate tryit">
-module(helloworld).
-export([start/0]).
start() ->
X = 40,
Y = 50,
result = X + Y,
io:fwrite("~w",[Result]).
</pre>
<p>If you try to compile the above program, you will get the following compile time error.</p>
<h3>Output</h3>
<pre class="result notraslate">
helloworld.erl:8: variable 'Result' is unbound
</pre>
<p>Secondly, all variables can only be assigned once. Let’s take an example of assigning a variable more than once.</p>
<h3>Example</h3>
<pre class="prettyprint notraslate tryit">
-module(helloworld).
-export([start/0]).
start() ->
X = 40,
Y = 50,
X = 60,
io:fwrite("~w",[X]).
</pre>
<p>If you try to compile the above program, you will receive the following compile time error.</p>
<h3>Output</h3>
<pre class="result notraslate">
helloworld.erl:6: Warning: variable 'Y' is unused
helloworld.erl:7: Warning: no clause will ever match
helloworld.erl:7: Warning: the guard for this clause evaluates to 'false'
</pre>
<h2>Printing Variables</h2>
<p>In this section we will discuss how to use the various functions of printing variables.</p>
<h3>Using the io:fwrite function</h3>
<p>You would have seen this (io:fwrite) used in all of the above programs. The <b>fwrite</b> function is part of the ‘io’ module or Erlang, which can be used to output the value of variables in the program.</p>
<p>The following example shows a few more parameters which can be used with the fwrite statement.</p>
<h3>Example</h3>
<pre class="prettyprint notraslate tryit">
-module(helloworld).
-export([start/0]).
start() ->
X = 40.00,
Y = 50.00,
io:fwrite("~f~n",[X]),
io:fwrite("~e",[Y]).
</pre>
<p>The output of the above program will be −</p>
<h3>Output</h3>
<pre class="result notraslate">
40.000000
5.00000e+1
</pre>
<title>Erlang Operators</title>
<h1>Erlang - Operators</h1>
<p>An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.</p>
<p>Erlang has the following type of operators −</p>
<h2>Arithmetic Operators</h2>
<p>Erlang language supports the normal Arithmetic operators as any the language. Following are the Arithmetic operators available in Erlang.</p>
<p><a href="/erlang/erlang_arithmatic_operators.htm">Show Examples</a></p>
<h2>Relational Operators</h2>
<p>The Relational Operators allow the comparison of objects. Following are the relational operators available in Erlang.</p>
<p><a href="/erlang/erlang_relational_operators.htm">Show Examples</a></p>
<h2>Logical Operators</h2>
<p>These Logical Operators are used to evaluate Boolean expressions. Following are the logical operators available in Erlang.</p>
<p><a href="/erlang/erlang_logical_operators.htm">Show Examples</a></p>
<h2>Bitwise Operators</h2>
<p>Erlang provides four bitwise operators. Following are the bitwise operators available in Erlang.</p>
<p><a href="/erlang/erlang_bitwise_operators.htm">Show Examples</a></p>
<p><b>band</b></p>
<p>This is the bitwise “and” operator</p>
<p><b>bor</b></p>
<p>This is the bitwise “or” operator</p>
<p><b>bxor</b></p>
<p>This is the bitwise “xor” or Exclusive or operator</p>
<p><b>bnot</b></p>
<p>This is the bitwise negation operator</p>
<p>Following is the truth table showcasing these operators −</p>
<h2>Operator Precedence</h2>
<p>The following table shows the Operator Precedence for the Erlang operators in order of descending priority together with their associativity. Operator precedence and associativity are used to determine the evaluation order in un-parenthesized expressions.</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<title>Erlang Loops</title>
<h1>Erlang - Loops</h1>
<p>Erlang is a functional programming language and what needs to be remembered about all functional programming languages is that they don’t offer any constructs for loops. Instead, functional programming depends on a concept called recursion.</p>
<h2>while Statement Implementation</h2>
<p>Since there is no direct while statement available in Erlang, one has to use the recursion techniques available in Erlang to carry out a while statement implementation.</p>
<p>We will try to follow the same implementation of the while loop as is followed in other programming languages. Following is the general flow which will be followed.</p>
<p>Let’s look at an example of how we can use recursion to implement the <b>while</b> loop in Erlang.</p>
<h3>Example</h3>
<p>The following key points need to be noted about the above program −</p>
<p>Define a recursive function called while which would simulate the implementation of our while loop.</p>
<p>Input a list of values defined in the variable X to our while function as an example.</p>
<p>The while function takes each list value and stores the intermediate value in the variable ‘Acc’.</p>
<p>The while loop is then called recursively for each value in the list.</p>
<p>The output of the above code will be −</p>
<h3>Output</h3>
<h2>for Statement</h2>
<p>Since there is no direct <b>for</b> statement available in Erlang, one has to use the recursion techniques available in Erlang to carry out a <b>for</b> statement implementation.</p>
<p>We will try to follow the same implementation of the <b>for</b> loop as is followed in other programming languages. Following is the general flow which should be adhered to.</p>
<p>Let’s look at an example of how we can use recursion to implement the <b>for</b> loop in Erlang.</p>
<h3>Example</h3>
<p>The following key points need to be noted about the above program −</p>
<p>We are defining a recursive function which would simulate the implementation of our <b>for loop</b>.</p>
<p>We are using a guard within the ‘for’ function to ensure that the value of N or the limit is a positive value.</p>
<p>We recursively call the for function, by reducing the value of N at each recursion.</p>
<p>The output of the above code will be −</p>
<h3>Output</h3>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="prettyprint notranslate tryit">
-module(helloworld).
-export([while/1,while/2, start/0]).
while(L) -> while(L,0).
while([], Acc) -> Acc;
while([_|T], Acc) ->
io:fwrite("~w~n",[Acc]),
while(T,Acc+1).
start() ->
X = [1,2,3,4],
while(X).
</pre>
<p>The following key points need to be noted about the above program −</p>
<ul class="list">
<li><p>Define a recursive function called while which would simulate the implementation of our while loop.</p></li>
<li><p>Input a list of values defined in the variable X to our while function as an example.</p></li>
<li><p>The while function takes each list value and stores the intermediate value in the variable ‘Acc’.</p></li>
<li><p>The while loop is then called recursively for each value in the list.</p></li>
</ul>
<p>The output of the above code will be −</p>
<h3>Output</h3>
<pre class="result notranslate">
0
1
2
3
</pre>
<h2>for Statement</h2>
<p>Since there is no direct <b>for</b> statement available in Erlang, one has to use the recursion techniques available in Erlang to carry out a <b>for</b> statement implementation.</p>
<p>We will try to follow the same implementation of the <b>for</b> loop as is followed in other programming languages. Following is the general flow which should be adhered to.</p>
<img src="/erlang/images/for_statement.jpg" alt="for Statement" />
<p>Let’s look at an example of how we can use recursion to implement the <b>for</b> loop in Erlang.</p>
<h3>Example</h3>
<pre class="prettyprint notranslate tryit">
-module(helloworld).
-export([for/2,start/0]).
for(0,_) ->
[];
for(N,Term) when N > 0 ->
io:fwrite("Hello~n"),
[Term|for(N-1,Term)].
start() ->
for(5,1).
</pre>
<p>The following key points need to be noted about the above program −</p>
<ul class="list">
<li><p>We are defining a recursive function which would simulate the implementation of our <b>for loop</b>.</p></li>
<li><p>We are using a guard within the ‘for’ function to ensure that the value of N or the limit is a positive value.</p></li>
<li><p>We recursively call the for function, by reducing the value of N at each recursion.</p></li>
</ul>
<p>The output of the above code will be −</p>
<h3>Output</h3>
<pre class="result notranslate">
Hello
Hello
Hello
Hello
Hello
</pre>
<title>Erlang Decision Making</title>
<h1>Erlang - Decision Making</h1>
<p>Decision making structures requires that the programmer should specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be <b>true</b>, and optionally, other statements to be executed if the condition is determined to be <b>false</b>.</p>