Download presentation
Presentation is loading. Please wait.
1
Basic Syntax and Semantics
Computer Science 325 Overview of Erlang I Basic Syntax and Semantics 1
2
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
3
Firing up the Erlang Shell
4
Numbers, Arithmetic, Comparisons
1> % Syntax: <expression>. 53 2> 3.14. 3.14 3> * 2. 11 4> 3 == 4. false 5> 3 =< 4. true Syntax is derived from Prolog, in which a set of clauses ends with a ‘.’
5
Numbers and Function Calls
6> abs(-2). 2 7> sqrt(2). ** exception error: undefined shell command sqrt/1 8> math:sqrt(2). 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
6
Booleans and Logic Note the =< for less than 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
7
Atoms and Strings 14> false. false 15> apple. 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
8
Variables and Assignment
18> Area = 3.14 * 6 * 6. 19> Area. 20> Apple. * 1: variable 'Apple' is unbound Single assignment only, for binding. Variables must be capitalized.
9
Assignment Is Really Pattern Matching
21> Area. 22> Area = 3.14 * 5 * 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
10
Tuples and Pattern Matching
21> Ken = {person, {name, ken}, {eyeColor, blue}}. {person, {name, ken}, {eyeColor, blue}} 22> Ken. {person, {name, ken}, {eyeColor, blue}}. 23> {person, {name, Name}, {eyeColor, EyeColor}} = Ken. {person,{name,ken},{eyeColor,blue}} 24> Name. ken 25> EyeColor. blue Tuples use {} as delimiters
11
Lists 26> Numbers = [100, 66, 33 22, 15]. [100, 66, 33 22, 15] 27> Numbers. 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.
12
Lists 26> Numbers = [100, 66, 33 22, 15]. [100, 66, 33 22, 15] 27> Numbers. If T is a list, then [H|T] is also a list, with head H and tail T. [] is the empty list. 28> MoreNumbers = [99 | Numbers]. % Like : in Haskell [99, 100, 66, 33 22, 15] 29> MoreNumbers.
13
Lists 32> MoreNumbers. [99, 100, 66, 33, 22, 15]
33> [One, Two, Three|Tail] = MoreNumbers. 34> One. 99 35> Two. 100 36> Three. 66
14
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 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.
15
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
16
An Example Module % geometry.erl -module(geometry). -export([area/1]). % Function name and arity area({rectangle, Width, Height}) -> Width * Height; % First clause area({circle, Radius}) -> * Radius * Radius. % 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
17
Usage of the Module A module must be compiled before it can be used.
% geometry.erl -module(geometry). -export([area/1]). % Function name and arity area({rectangle, Width, Height}) -> Width * Height; % First clause area({circle, Radius}) -> * Radius * Radius. % Second clause 1> c(geometry). {ok, geometry} 2> geometry:area({rectangle, 10, 5}). 50 3> geometry:area({circle, 1.4}). A module must be compiled before it can be used. Use c(<module name>) in the Erlang shell.
18
Another Example Compile and run from the terminal: % 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! $
19
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?
20
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
21
Anonymous Functions (funs)
1> fun(X, Y) -> X * Y end. #Fun<erl_eval > 2> Multiply = fun(X, Y) -> X * Y end. 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.
22
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]
23
Quitting the Shell When a process hangs, ctrl-c can exit the shell.
38> q(). % or halt(). When a process hangs, ctrl-c can exit the shell.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.