Make delicious recipes!

Erlang




  1. Variable names must begin with a capital letter.

  2. Each expression must end with a dot and a white space.

  3. '_' (underscore) is a special variable which can be assigned to any value but remains unbound when evaluated.
    It is used for 'do-not-care' type values and will become clear from below examples.

  4. '=' operaor assigns value to LHS only when it does not have a value assigned already or when the RHS is same as LHS.

  5. Variable names that begin with a small letter are called atoms and they behave like constant literals.
    Internally atoms are represented by an atom table, which uses one pointer space per atom for storage.
    The atom table is not garbage collected.

  6. 'true' and 'false' are atoms and they do not equal 0 and 1.

  7. Equality comparison operators are =:=, =/=, == and /=

  8. Order comparison operators are <, >, =< and >=
    (Note the odd way of specifying 'less-than-equal-to' sign)

  9. Tuples are a way to group data together by making use of curly braces.
    Example:
    
    erl> Point = {4,5}. 
    {4,5}
    
    erl> Dimension = {Point, 6}. 
    {{4,5},6}
    
    erl> Time = 'Tuesday' .              
    'Tuesday'
    
    erl> Dimension4 = {Dimension, Time}. 
    {{{4,5},6},'Tuesday'}
    
    erl> BigDim = {{Point, Dimension}, Dimension4}. 
    {{{4,5},{{4,5},6}},{{{4,5},6},'Tuesday'}}
    
    


  10. Lists are a way to group data even further by making use of square brackets.
    erl> [1,2,3] ++ [4,5]. 
    [1,2,3,4,5]
    
    erl> [1,2,3,4,5] -- [1,2,3]. 
    [4,5]
    
    erl> [1,2,3,4,5] -- [1,2,3] -- [4,5]. 
    [4,5]
    
    erl> [1,2,3,4,5] -- [1,2,3] ++ [4,5]. 
    []
    

  11. Every list has a head and a tail:
    erl> [Head | Tail] = [1,2,3,4]. 
    [1,2,3,4]
     
    erl> Head. 
    1
    
    erl> Tail. 
    [2,3,4]
    
    erl> [Head1 | Tail1] = Tail. 
    [2,3,4]
    
    erl> Head1. 
    2
    
    erl> Tail1. 
    [3,4]
    
    erl> [Ele1, Ele2 | Tail2] = [1,2,3,4]. 
    [1,2,3,4]
    
    erl> Ele1. 
    1
    
    erl> Ele2. 
    2
    
    erl> Tail2. 
    [3,4]
    
    

  12. Erlang provides great list building capabilities.
    For example, in the language of mathematics, a list or a set can be expressed as
    x = x2 where x in [1,2,3,4]
    The same can be expressed in Erlang as
    [x*x || x <- [1,2,3,4]]

    Drawing an equivalence between the above two expressions,
    || stands for where
    <- stands for in

    erl> [X+Y || X <- [1,2], Y <- [2,3]].
    [3,4,4,5]
    
    erl> People = [
      {john, smith, 26}, 
      {maria, smith, 30}, 
      {michael, brown, 45}, 
      {sylvester, brown, 30}, 
      {johnny, bravo, 35}
    ]. 
    [{john,smith,26},
     {maria,smith,30},
     {michael,brown,45},
     {sylvester,brown,30},
     {johnny,bravo,35}]
    
    
    erl> Smiths = [Name || {Name, smith, _} <- People]. 
    [john,maria]
    
    
    erl> Smiths2 = [Name || {Name, smith, Age} <- People, Age < 40]. 
    [john,maria]
    
    





Like us on Facebook to remain in touch
with the latest in technology and tutorials!


Got a thought to share or found a
bug in the code?
We'd love to hear from you:

Name:
Email: (Your email is not shared with anybody)
Comment:

Facebook comments:

Site Owner: Sachin Goyal