Computer Science 325 Overview of Erlang I Basic
Computer Science 325 Overview of Erlang I Basic Syntax and Semantics
Erlang Pragmatics • Expressions can be evaluated interactively in an interpreter • Programs consist of modules of function definitions, which must be saved in files, compiled, and imported for use
Firing up the Erlang Shell
Numbers, Arithmetic, Comparisons 1> 53. 53 % Syntax: <expression>. 2> 3. 14 3> 3 + 4 * 2. 11 4> 3 == 4. false 5> 3 =< 4. true Syntax is derived from Prolog, in which a set of clauses ends with a ‘. ’
Numbers and Function Calls 6> abs(-2). 2 7> sqrt(2). ** exception error: undefined shell command sqrt/1 8> math: sqrt(2). 1. 4142135623730951 9> % Syntax: <module name>: <function name> No need to import, if it’s a standard module Other modules must be in the current working directory
Booleans and Logic 10> 3 != 2. * 1: syntax error before: '=' 11> not (3 == 2). true 12> (7 >= 2) and (7 =< 6). false 13> false or true Note the =< for less than
Atoms and Strings 14> false 15> apple 16> "Erlang rules!". "Erlang rules!” 17> [1, 2, 3]. Atoms are like enumerated values in C, Java, or Haskell Must begin with a lowercase letter Lists are not atomic, so they don’t evaluate to themselves
Variables and Assignment 18> Area = 3. 14 * 6. 113. 03999999 19> Area. 113. 03999999 20> Apple. * 1: variable 'Apple' is unbound Single assignment only, for binding. Variables must be capitalized.
Assignment Is Really Pattern Matching 21> Area. 113. 03999999 22> Area = 3. 14 * 5. ** exception error: no match of right hand side value 78. 5 When Erlang sees the = operator it tries to match the expressions on the left and right, to answer the question, what can I do to make this statement true? A match won’t succeed if a variable already has a binding
Tuples and Pattern Matching 21> Ken = {person, {name, ken}, {eye. Color, blue}} 22> Ken. {person, {name, ken}, {eye. Color, blue}}. 23> {person, {name, Name}, {eye. Color, Eye. Color}} = Ken. {person, {name, ken}, {eye. Color, blue}} 24> Name. ken 25> Eye. Color. blue Tuples use {} as delimiters
Lists 26> Numbers = [100, 66, 33 22, 15] 27> Numbers. [100, 66, 33 22, 15] If T is a list, then [H|T] is also a list, with head H and tail T. [] is the empty list. | is like : in Haskell.
Lists 26> Numbers = [100, 66, 33 22, 15] 27> Numbers. [100, 66, 33 22, 15] If T is a list, then [H|T] is also a list, with head H and tail T. [] is the empty list. 28> More. Numbers = [99 | Numbers]. [99, 100, 66, 33 22, 15] 29> More. Numbers. [99, 100, 66, 33 22, 15] % Like : in Haskell
Lists 32> More. Numbers. [99, 100, 66, 33, 22, 15] 33> [One, Two, Three|Tail] = More. Numbers. [99, 100, 66, 33, 22, 15] 34> One. 99 35> Two. 100 36> Three. 66
Getting Help 37> help(). ** shell internal commands ** b() -- display all variable bindings e(N) -- repeat the expression in query <N> f() -- forget all variable bindings f(X) -- forget the binding of variable X h() -- history(N) -- set how many previous commands to keep results(N) -- set how many previous command results to keep catch_exception(B) -- how exceptions are handled v(N) -- use the value of query <N> rd(R, D) -- define a record rf() -- remove all record information rf(R) -- remove record information about R rl() -- display all record information rl(R) -- display record information about R rp(Term) -- display Term using the shell's record information. . . etc.
Modules • As in Python, Erlang programs consist of modules, which map directly to source (. erl) files • A module must be saved and compiled before its code can be run • Modules typically consist of function definitions and other executable code, such as startup code
An Example Module % geometry. erl -module(geometry). -export([area/1]). % Function name and arity area({rectangle, Width, Height}) -> Width * Height; area({circle, Radius}) -> 3. 14159 * Radius. % First clause % Second clause When the function is called, the actual argument is matched to the function’s parameter. Each clause is tried until a match occurs or an exception is raised. Think of the ; as an OR, without the Boolean result
Usage of the Module % geometry. erl -module(geometry). -export([area/1]). % Function name and arity area({rectangle, Width, Height}) -> Width * Height; area({circle, Radius}) -> 3. 14159 * Radius. 1> c(geometry). {ok, geometry} 2> geometry: area({rectangle, 10, 5}). 50 3> geometry: area({circle, 1. 4}). 6. 15752 A module must be compiled before it can be used. Use c(<module name>) in the Erlang shell. % First clause % Second clause
Another Example % hello. erl -module(hello). -export([start/1]). start() -> io: format("Hello world!~n"). Compile and run from the terminal: $ erlc hello. erl $ erl –noshell –s hello start –s init stop Hello world! $
Recursive List Processing % mylist. erl -module(mylist). -export([sum/1]). % Function name and arity sum(L) -> sum(L, 0). sum([], N) -> N; sum([H|T]) -> sum(T, H + N). Same function name, different arities. Tail recursive?
Recursive List Processing % mylist. erl -module(mylist). -export([sum/1]). % Function name and arity sum(L) -> sum(L, 0). sum([], N) -> N; sum([H|T], N) -> sum(T, H + N). 1> c(mylist). {ok, mylist} 2> mylist: sum([10, 20, 30]). 60
Anonymous Functions (funs) 1> fun(X, Y) -> X * Y end. #Fun<erl_eval. 12. 80484245> 2> Multiply = fun(X, Y) -> X * Y end. #Fun<erl_eval. 12. 80484245> 3> Multiply(2, 5). 10 funs can be passed as arguments to other functions and returned as their values. But other functions are not first-class data.
Map and Filter 1> lists: map(abs, [-1, 2, -3, -6]). ** exception error: bad function abs in function lists: map/2 (lists. erl, line 1224) 2> lists: map(fun(X) -> abs(X) end, [-1, 2, -3, -6]). [1, 2, 3, 6] 3> lists: filter(fun(X) -> X > 0 end, [-1, 2, -3, -6]). [2]
Quitting the Shell 38> q(). % or halt(). When a process hangs, ctrl-c can exit the shell.
- Slides: 23