CS 3060: Programming Languages

Erlang

Robert C. Green II, Ph.D.

Day 2

Conditionals

                        
Animal = "dog".

case Animal of
    "dog" -> underdog;
    "cat" -> thundercat
end.
                        
                    

Conditionals

                        
case Animal of
    "elephant" -> dumbo;
    _ -> something_else
end.
                        
                    

Conditionals

                        

X = 0.

if
    X > 0 -> positive;
    X < 0 -> negative
end.
                        
                    

Conditionals

                        

if
    X > 0 -> positive;
    X < 0 -> negative;
    true -> zero
end.
                        
                    

Anonymous Functions

                        
    Negate = fun(I) -> -I end.
                        
                    

Lists & Higher Order Functions

                        
Numbers = [1, 2, 3, 4].

lists:foreach(fun(Number) -> 
    io:format("~p~n", [Number]) end, Numbers).
                        
                    

Lists & Higher Order Functions

                        
Print = fun(X) -> io:format("~p~n", [X]) end.

lists:foreach(Print, Numbers).
                        
                    

Lists & Higher Order Functions

                        
lists:map(fun(X) -> X + 1 end, Numbers).
                        
                    
                        
map(F, [H|T]) -> [F(H) | map(F, T)];
map(F, [])    -> [].
                        
                    

Filters

                        
Small = fun(X) -> X < 3 end.

lists:filter(Small, Numbers).
                        
                    

Filters

                        
lists:all(Small, [0, 1, 2]).

lists:all(Small, [0, 1, 2, 3]).
                        
                    
                        
lists:any(Small, [0, 1, 2, 3]).

lists:any(Small, [3, 4, 5]).
                        
                    

Filters

                        
lists:any(Small, []).

lists:all(Small, []).
                        
                    

Filters

                        
lists:takewhile(Small, Numbers).

lists:dropwhile(Small, Numbers).

lists:takewhile(Small, [1, 2, 1, 4, 1]).
lists:dropwhile(Small, [1, 2, 1, 4, 1]).
                        
                    

foldl

                        
lists:foldl(fun(X, Sum) -> X + Sum end, 0, Numbers)
                        
                    

List Construction

On the surface, it may seem difficult to build lists without mutable state.
                        
-module(double).
-export([double_all/1]).

double_all([]) -> [];
double_all([First|Rest]) -> [First + First|double_all(Rest)].
                        
                    

List Comprehensions

                        
Fibs = [1, 1, 2, 3, 5].

Double = fun(X) -> X * 2 end.

lists:map(Double, Fibs).
                        
                    
                        
[Double(X) || X <- Fibs].

[X * 2     || X <- [1, 1, 2, 3, 5]].

                        
                    

List Comprehensions

                        
map(F, L) -> [ F(X) || X <- L].
                        
                    

List Comprehensions

                        
Cart = [{pencil, 4, 0.25}, {pen, 1, 1.20}, {paper, 2, 0.20}].
                        
                    
How would you write a comprehension to calculate tax at 8%?
                        
WithTax = [{Product, Quantity, Price, Price * Quantity * 0.08} ||
           {Product, Quantity, Price} <- Cart].
                        
                    

List Comprehensions

                        
Cat = [{Product, Price} || {Product, _, Price} <- Cart].
                        
                    
How would you write a comprehension to discount at 25%?
                        
DiscountedCat = [{Product, Price * .75} || {Product, Price} <- Cat].
                        
                    

List Comprehensions

  • A list comprehension takes the form of [Expression || Clause1, Clause2,..., ClauseN].
  • List comprehensions can have an arbitrary number of clauses.
  • The clauses can be generators or filters.
  • A filter can be a boolean expression or a function returning a boolean.
  • A generator, of the form Match <-List , matches a pattern on the left to the elements of a list on the right.

Assignment

Start the Assignment

Read Day 3

Resources