DEV Community

jdfolino
jdfolino

Posted on

Day 1: Erlang Hello World

Ok so after being a Java and Ruby guy for 13 years it is time for me to learn some Erlang for my new job.

Hello World in Ruby is extremely easy. You just open up the terminal and run the code below:

puts 'Hello World'

My initial attempt to do something similar in Erlang found no simple one liner to do so. Below is Hello World in Erlang:

% hello.erl
-module(hello).
-export([hello_world/0]).

hello_world() -> io:fwrite("hello, world\n").

My inner nerd found a few things to get excited about:

  • You finish each line with a full stop. This is just so logical and makes sense. When I started with java 13 years ago I always wondered why they used a semi colon instead of a full stop.
  • You declare the function to export with the arity. This is cool because you can see at glance how many arguments the function will take
  • Function declaration on a single line could mean we have nice, concise code

How do you run this code? You open the Erlang shell, compile the code in the shell (never seen that before) and then you call the function:

1> c(hello).
{ok,hello}

2> hello:hello_world().
hello, world

c(hello). literally compiles the hello.erl file. Another neat thing is how each time you evaluate a line it updates the number in the shell.

Top comments (4)

Collapse
 
epsi profile image
E.R. Nurwijayadi • Edited

Thank you posting.

I'm a beginner in Erlang. To help more beginner, I have made a working example with source code in github.

đź•· epsi.bitbucket.io/lambda/2020/11/1...

First the process module

Sender and Receiver

And then run both

Run Both in Program Entry Point

I hope this could help other who seeks for other case example.

🙏🏽

Thank you for posting with thorough explanation.

Collapse
 
ksaaskil profile image
Kimmo Sääskilahti

Maybe it's more fair to compare to Java :) Why Erlang by the way, could you use Elixir instead? Can one mix Erlang and Elixir like one can mix Java and Scala?

Collapse
 
jdfolino profile image
jdfolino

Oh I published it by accident, oops. I have a job where we are migrating from Ruby on Rails to Elixir. The hardcore Elixir guys said to me to understand Erlang as Phoenix will be easy for me (as it is so similar to Rails). And because we are in lockdown (damn COVID-19) I thought why not learn Erlang

Collapse
 
szajbus profile image
Michał Szajbe

Yes, Elixir compiles to Erlang. You can easily use Erlang directly from Elixir.