Git Product home page Git Product logo

erl's Introduction

Running erlang programs

For simple programs, use escript

Here is a simple hello world in escript:

#!/usr/bin/env escript
-export([main/1]).

main([]) -> io:format("Hello, World!~n").

for complex programs however, compile the standard way

source code :

-module(hello).
-export([start/0]).

start() ->
  io:format("Hello, World!").

from the shell comple & run like so :

$erl
>c(hello).
>hello:start().

to compile from a Emakefile do :

$ cd /path/to/emakefile
$ erl -make

Common syntax mistakes

  • Not using comma in case of multi line statement:

    X = 32 <-- missing comma
    Y = 38 
    
    compiles to give the followoing error::
    
    lists_bit_syntax.erl:7: syntax error before: Y
  • Forgetting that io:format takes a list to substitute values.:

    io:format("Value of X and Y are ~p , ~p" , X,Y) % Wrong, X,Y should be in a list
    io:format("Value of X and Y are ~p , ~p" , [X,Y]) % Correct
  • Not closing function clauses with semi-colon ( last one will close with a "."):

    valid_time({Y,M,D),{H,Min,S})->
        io_format("Good") . <-- There should be a semi-colon here instead of dot as it is part of clause
    valid_time(_) ->
        io:format("Invalid input"). <- last function in the clause ends with dot 
  • look out for less than equals and greater than equals:

    <= wrong =< right
    >= right => wrong 
  • forgetting semi-colon in both if and case ... of clauses and adding semi-clon in the last clause:

    if X == vala ->
        Res = call_some_function(X),
        Res; <--- this semi-colon should be there for all-but-last if clause
    if X == valb -> 
        Res = call_some_other_function(X),
        Res <--- last clause should not have anything. It is the end that completes the if block
    end,
    
    % case can let us do pattern matching 
    case X of ->
        vala ->
            Res = call_some_function(X),
            Res;
        valb ->
            Res = call_some_other_function(X),
            Res
    end.  
  • ending the last statement of an anonymous funtion with a period. In a normal fuction last statement is followed by a period:

    % Normal function 
    do_something(X) ->
      io:format("Printing value of X ~p" ,[ X ] ),
      io:format("Doing somethiing~n"). % <-------- notice the period here
    
    DoSomething = fun(A) -> 
      io:format("Printing value of X ~p" ,[ X ] ),
      io:format("Doing somethiing~n") %<--------  no period is required 
    end % <------ , or . depending on where it is 
  • putting -> after receive or leaving -> after after :

    % this is a bit weird but have to live with the syntax
    % there should be no '->' after the receive and there should be
    % a '->' after the 'after'
    receive 
      { From, Msg } -> do_something(Msg)
    after 2000 -> 
      timeout
    end,
  • be very careful with records :

    % observe how records are used in the function head and the return 
    % see the '=' sign 
    -record( state, { name="testing", 
                        value=0,
                        flags }.
    increase_val( S=#state{ value=V, name=Name}, IncVal) ->        % notice the '=' here
        %increases the value of state by IncVal amount
        io:format("increasing value of ~p by ~p~n",[Name,IncVal]),
        S#state{ value=V+Inc}.                                     % no '=' in the return value

Useful libraries and man pages

Resources and other stuff

erl's People

Contributors

syed avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.