Erlang for Python programmers: Part II
Ruslan Spivak - - September 17, 2007In this short tutorial we will take a look at comparison operations, some arithmetic operations and modules. You may also want to skim over Inro and Part I.
Comparison operations
Python Erlang Description Erlang Example
--------+--------+-----------------------+----------------
< < strictly less than
--------+--------+-----------------------+----------------
<= =< less than or equal
--------+--------+-----------------------+----------------
> > strictly greater than
--------+--------+-----------------------+----------------
>= >= greater than or equal
--------+--------+-----------------------+----------------
!= /= not equal
--------+--------+-----------------------+----------------
== == equal 1> 1 == 1.
true
2> 1 == 1.0.
true
--------+--------+-----------------------+----------------
=:= exactly equal to 1> 1 =:= 1.
true
2> 1 =:= 1.0.
false
--------+--------+-----------------------+----------------
=/= exactly not equal to
--------+--------+-----------------------+----------------
is object identity
--------+--------+-----------------------+----------------
is not negated obj identity
Python notes
Above comparison operations are supported by all objects. In Python you can chain comparisons:
>>> x, y, z = 1, 3, 7 >>> x < y <= z True >>> x < y and y <= z True
Foregoing examples are identical, except that in second case y is evaluated twice.
Erlang notes
Following order is defined:
number < atom < reference < fun < port < pid < tuple < list < binary
1> 5 < erlang. true 2> erlang < make_ref(). true
Both Erlang comparison operators (<, =<, >, >=, /=, ==) and Python comparison operators(<, <=, >, >=, !=, ==) make type coerce, “narrower” type is widened to that of another, ie when comparing integer and float first integer is converted to float, etc.
Erlang has special operators without type coerce though: =:= and =/=
Arithmetic operations
I’ll provide only several operations, for more take a look at corresponding language references.
Python Python Desc. Erlang Erlang Desc. Example
--------+--------------------+---------+--------------+--------------------
x % y remainder X rem Y integer >>> 7 % 3
of x / y remainder 1
of X / Y >>> 7.0 % 3
1.0
1> 7 rem 3.
1
1> 7.0 rem 3.
=ERROR REP..
--------+--------------------+---------+--------------+--------------------
x / y quotient X / Y floating >>> 7 / 3
of x and y point 2
division >>> 7.0 / 3
2.3333333333333335
1> 7 / 3.
2.33333
--------+--------------------+---------+--------------+--------------------
x // y (floored) quotient X div Y integer >>> 7 // 3
of x and y, division 2
integer division >>> 7.5 // 3
(result type is 2.0
not forced to be
int) 1> 7 div 3.
2
Modules
Code in Erlang is organized into units called modules, which is familiar word for Python programmer.
Let’s define sample module with function declaration stored in file mymath.erl :
-module(mymath). -export([fact/1]). fact(0) -> 1; fact(N) -> N * fact(N-1).
What we see here:
- module’s source code is stored in file with .erl extension
- module consists of attributes and function declarations which are terminated by period (.)
- we should provide module declaration defining name of the module
- module name should be atom
- module name is the same as file name minus .erl extension
- module declaration is mandatory
- module declaration attribute should be defined first.
To make functions defined in module accessible outside the module we need to export them, for this -export module attribute exists. We write exported functions inside square brackets in form of func/N where N is the number of arguments of function, called arity. I’ll repeat that functions not pointed in -export will not be accessible outside module.
Before code can be run, module must be compiled. Resulting compiled file will contain extension .beam
If you use Emacs you can compile module with C-c C-k and see results in erlang shell:
1> c("/home/alienoid/dev/erlang/mymath", [{outdir, "/home/alienoid/dev/erlang/"}]). {ok,mymath}
Or you can compile it directly in shell. Make sure your shell’s current directory is where your mymath.erl lives, if it’s not the case use cd command in erlang shell, cd(“/path/to/dir/with/mymath.erl”) :
1> c(mymath). {ok,mymath}
To invoke our function fact we use syntax mod:func :
2> mymath:fact(4). 24
Erlang has also -import attribute which allows to import functions into modules, so that you don’t need to use fully-qualified name mod:func to invoke function, again familiar behaviour and naming to Python programmer.
Erlang allows to insert code from file as-is with -include attribute at point where -import is defined, this is used to include records and macro definitions, for example.
Comments in Erlang module begin with character “%“, continue up to end-of-line and may be placed anywhere except inside string and quoted atoms.
Like in Python Erlang has no multiline comments.
If you use Emacs you can easily comment whole region with M-; command after you marked it.
-module(mymath). -export([fact/1, print_double/1]). -import(lists, [foreach/2]). -include("my_records.hrl"). %% sum(L) -> %% sum(L, 0). %% sum([H|T], Acc) -> %% sum(T, H+Acc); %% sum([], Acc) -> Acc. fact(0) -> 1; fact(N) -> N * fact(N-1). print_double(L) -> foreach(fun(X) -> io:format("Double of ~p = ~p~n", [X, X*2]) end, L).
Fin.
Next tutorial will be devoted to thorough exploration of functions in Erlang.
Categories: Blogs Ruslan Spivak
Erlang on Twitter
» macintux (John Daily): RT @cmeik: LMAO if you don’t work with Big Nosk and can call him at 10:30 PM EDT for help on fixing an Erlang problem. I love my job and c…
» tsantero (Tom Santero): RT @cmeik: LMAO if you don’t work with Big Nosk and can call him at 10:30 PM EDT for help on fixing an Erlang problem. I love my job and c…
» Erlang_07 (Erlang ): @VanDanz14 gmn?
» cmeik (Chris Meiklejohn): LMAO if you don’t work with Big Nosk and can call him at 10:30 PM EDT for help on fixing an Erlang problem. I love my job and coworkers.
» macintux (John Daily): RT @developerevents: Intro to Erlang for C# Developers - 20 May - 170 Graphics Drive, USA http://t.co/LCJ6ritUv8 #csharp #event
» ErlangInfo (Erlang!): RT @developerevents: Intro to Erlang for C# Developers - 20 May - 170 Graphics Drive, USA http://t.co/LCJ6ritUv8 #csharp #event
» ErlangInfo (Erlang!): RT @SNTalentHiring: Lead Server Core Developer (Java/Erlang) http://t.co/IFaqmh3y9O #empleo #Barcelona #Internet #tecnología #TIC
» ErlangInfo (Erlang!): RT @diegosevilla: At last I’ve had the time to release it: uForth interpreted using model-driven techniques (#MDE) and #Erlang: https://t.c…
» ErlangInfo (Erlang!): RT @huntugtweets: HUNTUG meeting, Monday, May 20th, 6pm - “Intro to Erlang for C# Developers” by Ineta speaker Bryan Hunter.
» Adhika_AN (Adhika Nugroho ): Iyo ngga di kantor DPRD lamo”@Muh_Erlang: @Adhika_AN hari ini ado dahsyat yo? Macet pasti?”
Statistics
Number of aggregated posts: 10648
Most recent article: May 14, 2013
Latest comments
» Moraru on This is Why You Spent All that Time Learning to Program: It is true that computer science was a pain in the back at time that i’ve had to learn it…
» Commercial hand dryers on Couchbase Meetup at new HQ: Buy online from here where you will get so much of variety in Commercial hand dryers for people. If you…
» Fort McMurray Homes on Motivated Reasoning and Erlang vs Python vs Node: I don’t really understand why this post is motivational? I don’t even see a post, just a title. Fort McMurray…