Skip to content

Latest commit

 

History

History
197 lines (130 loc) · 5.89 KB

day_1.md

File metadata and controls

197 lines (130 loc) · 5.89 KB

Erlang tutorial day 1

Table of Contents

  1. Introduction
  2. Setup
  3. Erlang shell, variables, types, modules, functions
  4. Exercises
  5. Recommendation

1 Introduction

This tutorial

Learn You Some Erlang book Etudes for Erlang book

Erlang

  • History: Makes it easier to build scalable, fault-tolerant systems
  • Functional programming language with dynamic typing
  • Processes, node → concurrency, multi-core, distribution
  • Tracing and hot code loading

Agner Krarup Erlang

2 Setup

2.1 Setup

Installation

  • Windows: http://www.erlang.org/downloads
  • Linux/Mac packages from Erlang Solutions
  • Linux (usually not the newest): apt-get install erlang, yum install erlang
  • Mac: brew install erlang, port install erlang
  • kerl
    • for Linux and Mac
    • installs Erlang from source
    • handles multiple Erlang versions
    • bashrc config:
      export KERL_DEFAULT_INSTALL_DIR=$HOME/.kerl/installations/
      export KERL_ENABLE_PROMPT=true

Editor/IDE: Emacs, Vim, IntelliJ IDEA, Visual Studio Code, Sublime, ...

3 Erlang shell, variables, types, modules, functions

The Shell: werl.exe (Windows), erl (anything else)

Shell Commands:

  • Up/down (history), ctrl-a (home), ctrl-e (end)
  • Terminate expressions with a period (.)
  • Quit: q() or ctrl-c a

Numbers: +, -, *, /, div, rem

Invariable Variables

  • One = 1
  • Variables start with a capital letter
  • =: assignment (it actually does pattern matching)
  • We don't change variables; we bind them
  • f(), f(Var): forget variables in the shell

Atoms: atom, 'Special atom'

Boolean Algebra & Comparison operators

  • true, false
  • =:=, =/=

Tuples

  • Point = {4, 5}.
  • element(1, Point).

What are modules: lists:seq(1, 4).

Module declaration: see useless.erl

Compiling the code

> c(useless).
> useless:greet_and_add_two(-3).
> useless:module_info().

Typing Functions

4 Exercises

Exercise 1: Installation

Install Erlang.

Exercise 2: Shell

Étude 1: Getting Comfortable with Erlang.

Exercise 3: Geometry

Étude 2: Functions and Modules.

Exercise 4: One Year Long Mission

Use the shell to calculate how many months did the "ISS One Year Long Mission" take, considering that it took 342 days. (If you) are stuck: first calculate the length of the average month.)

Example solution.

Exercise 5: Temperature

Extra 2: Write functions temp:f2c(F) and temp:c2f(C) which convert between centigrade and Fahrenheit scales. (Source.)

If you want to convert Celsius to Fahrenheit, just multiply the temperature by 1.8 and then add 32 to the product. If you want to convert temperature from Fahrenheit to Celsius, subtract 32 from the number and then divide the difference by 1.8.

Don't forget to add type specs and docstrings.

Example solution.

Exercise 6: Time

  1. Write the function time:hms_to_seconds(H, M, S) that converts a time duration given as hours:minutes:seconds into seconds. E.g.

  2. Implement the following inverse functions:

    time:seconds_to_h(Seconds)
    time:seconds_to_m(Seconds)
    time:seconds_to_s(Seconds)

    The following must hold for all non-negative integers:

    time:hms_to_seconds(time:seconds_to_h(Seconds),
                        time:seconds_to_m(Seconds),
                        time:seconds_to_s(Seconds)) =:= Seconds
  3. Implement the inverse function time:seconds_to_hms(Seconds), which returns a {H, M, S} tuple. The following must hold for all non-negative integers:

    H = element(1, time:seconds_to_hms(Seconds)),
    M = element(2, time:seconds_to_hms(Seconds)),
    S = element(3, time:seconds_to_hms(Seconds)),
    time:hms_to_seconds(H, M, S) =:= Seconds

Don't forget to add type specs and docstrings.

Example solution.

5 Recommendation

Watch "Erlang: The Movie". It's only 11 minutes:

Erlang: The Movie (Youtube)