COSC441. Lecture 8 Introduction to Erlang

Similar documents
MTAT Agile Software Development

An Introduction to Erlang

On Academic Dishonesty. Declarative Computation Model. Single assignment store. Single assignment store (2) Single assignment store (3)

Introduction to Erlang. Franck Petit / Sebastien Tixeuil

Advanced Functional Programming, 1DL Lecture 2, Cons T Åhs

Functional programming in Erlang

Introduction to Erlang

DISTRIBUTED SYSTEMS [COMP9243] Lecture 1.5: Erlang INTRODUCTION TO ERLANG BASICS: SEQUENTIAL PROGRAMMING 2. Slide 1

All you need is fun. Cons T Åhs Keeper of The Code

Erlang Run-Time System Application (ERTS)

Programming Language Concepts, cs2104 Lecture 04 ( )

An Introduction to Erlang. Richard Carlsson

CS 11 Haskell track: lecture 1

An Introduction to Erlang

CS390 Principles of Concurrency and Parallelism. Lecture Notes for Lecture #5 2/2/2012. Author: Jared Hall

Programming Paradigms

CS 11 java track: lecture 1

DaMPL. Language Reference Manual. Henrique Grando

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

Elixir Documentation. Release. Elixir community

Erlang 101. Google Doc

Programming Language Concepts, cs2104 Lecture 01 ( )

Variables, Constants, and Data Types

The Actor Model. CSCI 5828: Foundations of Software Engineering Lecture 13 10/04/2016

LECTURE 3 C++ Basics Part 2

CMSC 330, Fall 2013, Practice Problem 3 Solutions

3. Java - Language Constructs I

An Introduction to Processing

CSE 12 Week Eight, Lecture One

Question No: 1 ( Marks: 1 ) - Please choose one One difference LISP and PROLOG is. AI Puzzle Game All f the given

Basics of Java Programming

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

CS 457/557: Functional Languages

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Lecture 5: Declarative Programming. The Declarative Kernel Language Machine. September 12th, 2011

LFE - a lisp on the Erlang VM

Functional Programming in Haskell Prof. Madhavan Mukund and S. P. Suresh Chennai Mathematical Institute

Computer Components. Software{ User Programs. Operating System. Hardware

To figure this out we need a more precise understanding of how ML works

Full file at

CPS506 - Comparative Programming Languages Elixir

printf( Please enter another number: ); scanf( %d, &num2);

Defining Functions. CSc 372. Comparative Programming Languages. 5 : Haskell Function Definitions. Department of Computer Science University of Arizona

Full file at

Briefly describe the purpose of the lexical and syntax analysis phases in a compiler.

Web Application Development (WAD) V th Sem BBAITM(Unit-1) By: Binit Patel

CS260 Intro to Java & Android 03.Java Language Basics

Mobile Computing Professor Pushpedra Singh Indraprasth Institute of Information Technology Delhi Andriod Development Lecture 09

Stacks and queues (chapters 6.6, 15.1, 15.5)

CS 11 Ocaml track: lecture 2

Values, Variables, Types & Arithmetic Expressions. Agenda

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Parallel Programming in Erlang (PFP Lecture 10) John Hughes

CSE3322 Programming Languages and Implementation

Erlang: An Overview. Part 2 Concurrency and Distribution. Thanks to Richard Carlsson for most of the slides in this part

Functional Programming Languages (FPL)

CMSC 330: Organization of Programming Languages. Rust Basics

Python I. Some material adapted from Upenn cmpe391 slides and other sources

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto

Common LISP Tutorial 1 (Basic)

a little more on macros sort of like functions, but..

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Begin at the beginning

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

Chapter 11 :: Functional Languages

A Language for Specifying Type Contracts in Erlang and its Interaction with Success Typings

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Lecture #23: Conversion and Type Inference

Tuples. CMSC 330: Organization of Programming Languages. Examples With Tuples. Another Example

Formal Methods of Software Design, Eric Hehner, segment 24 page 1 out of 5

To figure this out we need a more precise understanding of how ML works

6.001 Notes: Section 1.1

Core Erlang language specification

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

Lecture #24: Programming Languages and Programs

Erlang. Types, Abstract Form & Core. Salvador Tamarit Muñoz. Universitat Politècnica de València

ffl The textual representation should be easy for humans to read and edit, in case the developer wants to check what the Erlang source looks like in C

NOTE: Answer ANY FOUR of the following 6 sections:

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

This lecture covers: Prolog s execution strategy explained more precisely. Revision of the elementary Prolog data types

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

Well, Hal just told us how you build robust systems. The key idea was-- I'm sure that many of

Haskell An Introduction

CS 11 Ocaml track: lecture 7

PROgramming in LOGic. Part II. By Forrest Pepper 12/7/07

SML A F unctional Functional Language Language Lecture 19

Final-Term Papers Solved MCQS with Reference

ERLANG BITS AND PIECES

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

Today: Revisit some objects. Programming Languages. Key data structure: Dictionaries. Using Dictionaries. CSE 130 : Winter 2009

Ruby: Introduction, Basics

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Logical Methods in... using Haskell Getting Started

COP4020 Programming Assignment 1 - Spring 2011

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

CS 11 C track: lecture 6

Today in CS161. Week #3. Learn about. Writing our First Program. See example demo programs. Data types (char, int, float) Input and Output (cin, cout)

2. You are required to enter a password of up to 100 characters. The characters must be lower ASCII, printing characters.

Transcription:

COSC441 Lecture 8 Introduction to Erlang

Approach The reference book is Learn You Some Erlang for Great Good! by Fred Hébert. http://learnyousomeerlang.com lets you read it free on-line. What I am going to do is to introduce you to some of the key ideas; turn to the book for details. https://www.erlang.org/docs reference material

What goes into a language? Common ideas Application-specific ideas Designer quirks Accidents of history

Whence Erlang? Erlang was invented by Joe Armstrong, who was familiar with a wide range of imperative, OO, and declarative languages, specifically including Lisp, Prolog, and Strand-88. It was invented at the Ericsson Computer Science Laboratory. It was invented for programming soft real-time distributed telecoms applications, and secondarily for Internet applications.

Common ideas What do we need for a programming language? Minimally, a few combinators. I x = x K x y = x S x y z = x z (y z) https://en.wikipedia.org/wiki/ski_combinator_calcul us What about numbers, sequences, &c? We can model those as functions!

Common ideas 2 We need some built-in data types. We need operations for constructing them, deconstructing them, deriving new ones, and so on. We need a way to give names to values. We need a way to define and name functions. We need some form of conditional construct.

Built-in data types Integer (unbounded) Float (IEEE 754 doubles) Atoms (uniquely stored strings) Lists [] (empty) [Head Tail] (non-empty) Tuples {X1,...,Xn} Maps #{Key=>Val,, Key=>Val} Functions as values

Application data types Binaries (originally byte sequences, now bit sequences) for shipping uninterpreted packets around. Process IDs References (unforgeable unique cookies ) Ports (I/O connections that look a lot like pids)

Variables In the shared memory model, we saw that shared mutable data can be trouble. Let's not have any mutable variables! (There are actually several mutable bulk stores: the file system, the module system, the process registry, and several kinds of key-value store.) X = 1 is not an assignment statement!

Variables 2 A don't care variable or wild card is written as a single underscore _ Named variables are identifiers beginning with a capital letter. A variable is not a box you can change, but an alias for a value. A variable can become bound once and only once. See also Single Assignment C.

Matching 1 Variable = Expression evaluates Expression to V if Variable has no value yet, binds it to V if Variable has a value, checks whether that value is equal to V. If it is not, raises an exception. The whole form has V as its value, so (X = 1) + 1 has the value 2.

Static or dynamic typing? One purpose of type checking is to ensure that the whole program is consistent. One of the core requirements for Erlang was to support hot loading where a module can be replaced while the program is running. It is common for Java to load classes at run time, but Java does not replace classes dynamically Since Erlang doesn't have the whole program, it was originally designed without a type system.

Recognisers If you don't have types, you need recognisers. is_integer/1, is_float/1, is_number/1, is_atom/1, is_list/1, is_tuple/1, is_function/1, is_function/2, is_reference/1, is_pid/1, is_binary/1. We refer to the function called name with arity arguments in module as module:name/arity or name/arity for short. A recogniser returns the atom true or false.

Common operations The usual arithmetic operations Comparison. number < atom < reference < fun < port < pid < tuple < list < binary. Two sets of equality operators: =:= and =/= require type match as well as value match, while == and /= will equate integers and floats. Trap: equal-to-or-less-than is spelled =<. Erlang got this from Strand-88 which got it from Prolog which got it from Pop-2 (1967).

Conversions Several built-in operations convert a value from a source type to a target type. The convention is that such operations are called source_to_target/1. foo is a string. A string is just a list whose elements are Unicode code-points. << foo >> is a binary. This will be a sequence of 3 bytes. list_to_binary( foo ) => << foo >> binary_to_list(<< foo >>) => foo

Functions fun (Arguments) -> Body end is an anonymous function. myfun(arguments) -> Body. is a named function. Notice the full stop at the end. Every Erlang top level form must end with a full stop. Arguments are passed by pattern matching.

Creating a new process spawn(function_with_no_arguments) creates a new process executing the given function and returns its process ID. To find your own process ID, use self(). The new process does not know anything about its parent unless you tell it. There is no equivalent of getppid().

Communication Pid! Message sends Message to the process named by Pid. Pid can be a process id. Or an atom referring to a process in the registry. Send a message to a dead process and it just quietly disappears. If Pid is not a pid or a registered name, that's an error. Message can be any value, including a function.

Receiving a message receive Pat [when Guard] -> Body ; Pat [when Guard] -> Body ; ; after Timeout -> Body end.