Erlang: Your New Favorite Scripting Language?

What’s cool about Erlang?

Scalability

Image by joguldi

Ok, but what do I do with it?

Elegance

Image by Stephen A. Wolfe

Procedural

Narrative

Image from Pratham Books

Object-Oriented

Things that do things

Image from Frédéric Bisson

Relationships

Image from TheIguana

Functional

f(x)

What does that even mean?

OO vs. Functional

Image from Yun Huang Yong

Why?

Procedural

Why?

Procedural

Functional

Where are my curly braces?!

    my_func(Param1, ... ) ->
        %% intermediate stuff
        Value.

    f(X) -> X * 2.

Overload

Image by Graeme Newcomb

    my_func(Param1) ->
        my_func(Param1, "default").

    my_func(Param1, Param2) ->
        %% actually do stuff...

    my_func([]) -> ...

    my_func([Only]) -> ...

    my_func([First, Second | Rest]) -> ...

    my_func(0) -> ...

    my_func(X) where X > 0 -> ...

    my_func(X) where X < 0 -> ...

    if
        X >= 100 -> 100;
        X >= 0   -> X;
        true     -> 0
    end

    case my_func(X) of
        error -> [];
        List -> List
    end

    Output = case file:open(Filename, [read]) of
        {ok, Handle} ->
            process_file(Handle);
        {error, Reason} ->
            log_error(Reason, Filename),
            []
    end.

    #!/usr/bin/escript

    main(_Args) ->
        io:format("Hello World!~n").

    #!/usr/bin/escript

    main(Args) -> main("World", Args).

    main(_, ["-h" | _Args]) ->
        io:format("Help! Help!~n");

    main(_, ["-n", Name | Args]) ->
        main(Name, Args);

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

Detour

Image by chrisdlugosz

       project
            action
            sub-project
                action
            action
        project
            sub-project
                sub-project
                    action

* work on presentation [computer, quiet] (2011-08-24) !!

Input

Output

Ruby

Image from Wikimedia Commons

ruby parser

Hackers & Painters

Image from O’Reilly Media

Train Wreck

Image from IMLS DCC

    String[] process(String[] input) {
        String[] output = new String[input.length];
        for (int i=0; i < input.length; i++) {
            output[i] = do_stuff(input[i]);
        }
        return output;
    }

    process([First|Rest], Output) ->
        NewFirst = do_stuff(First),
        process(Rest, [NewFirst|Output]);

    Output = process(Input, []).

    process([First|Rest], Output) ->
        NewFirst = do_stuff(First),
        process(Rest, [NewFirst|Output]);

    process([], Output) ->
        lists:reverse(Output).

thinking

Image by Brian Hillegas

more thinking

Image by Alex E. Proimos

lisp step 1

lisp step 2

lisp step 3

lisp step 4

lisp step 5

Erlang: A kinder, gentler Lisp?

    build_nodes([{Content, Indent}|Rest]) ->
        IsChild = fun({_C, I}) -> I > Indent end,
        {ChildLines, SiblingLines} = lists:splitwith(IsChild, Rest),
        Children = build_nodes(ChildLines),
        Siblings = build_nodes(SiblingLines),
        [{Content, Children}|Siblings];

    build_nodes([]) -> [].

bowling

Image by Sam Howzit

Python

Image from Wikipedia

    score(Rolls) -> frame(Rolls, 1, 0).

    frame(_BonusRolls, 11, Score) -> Score;

    frame([10|Rest], Frame, Score) ->
        [Bonus1, Bonus2|_] = Rest,
        frame(Rest, Frame + 1, Score + 10 + Bonus1 + Bonus2);

    frame([First,Second|Rest], Frame, Score) when (First+Second==10)->
        [Bonus1|_] = Rest,
        frame(Rest, Frame + 1, Score + 10 + Bonus1);

    frame([First,Second|Rest], Frame, Score) ->
        frame(Rest, Frame + 1, Score + First + Second).

    test() ->
        test(0,   [0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0]),
        test(20,  [1,1, 1,1, 1,1, 1,1, 1,1, 1,1, 1,1, 1,1, 1,1, 1,1]),
        test(150, [5,5, 5,5, 5,5, 5,5, 5,5, 5,5, 5,5, 5,5, 5,5, 5,5, 5]),
        test(47,  [1,1, 1,1, 1,1, 1,1, 1,1, 1,1, 1,1, 1,1, 1,1, 10, 10 ,9]),
        test(173, [7,3, 7,3, 7,3, 7,3, 7,3, 7,3, 7,3, 7,3, 7,3, 7,3, 10]),

    test(Expected, Rolls) ->
        case score(Rolls) of
            Expected -> io:fwrite("Pass~n");
            Scored -> io:fwrite("Fail: expected=~p, scored=~p~n",
                [Expected, Scored])
        end.

Reflection

Image by Foxtongue

Bonsai

Image by Cass Chin

Fun

Image by orsocurioso

Start Small

Image by Shedking

Scale

Image by dullhunk

The End

Image by somethingcommon

Colin MacDonald