Programming Paradigms

Similar documents
Programming Paradigms

Programming Paradigms

An Introduction to Erlang. Richard Carlsson

Programming Paradigms

COP4020 Programming Assignment 1 - Spring 2011

Introduction to Erlang. Franck Petit / Sebastien Tixeuil

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

Programming Paradigms

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

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

Programming Paradigms

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Shell CSCE 314 TAMU. Higher Order Functions

Programming Paradigms

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

Chapter 1. Fundamentals of Higher Order Programming

CS 440: Programming Languages and Translators, Spring 2019 Mon

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

Documentation for LISP in BASIC

Functional Programming - 2. Higher Order Functions

Programming Paradigms Written Exam (6 CPs)

Introduction to Erlang. Franck Petit / Sebastien Tixeuil

Introduction to Erlang

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

A general introduction to Functional Programming using Haskell

Introduction to Functional Programming in Racket. CS 550 Programming Languages Jeremy Johnson

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Haskell Programming

CSCE 314 Programming Languages

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

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

Let s Get Functional. Learn You Some Erlang for Great Good! 2013, Fred Hébert

Getting Started with Erlang. version 5.6

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Programming Distributed Systems

Scheme: Expressions & Procedures

Lab 7: OCaml 12:00 PM, Oct 22, 2017

Programming Paradigms and Languages Introduction to Haskell. dr Robert Kowalczyk WMiI UŁ

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. Common Lisp Fundamentals

Functional Programming and Haskell

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II)

Scheme Quick Reference

Programming Paradigms

Functional Programming Languages (FPL)

Racket Pattern Matching

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

Prolog Introduction. Gunnar Gotshalks PI-1

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

Scheme Quick Reference

A Third Look At ML. Chapter Nine Modern Programming Languages, 2nd ed. 1

LECTURE 16. Functional Programming

An introduction introduction to functional functional programming programming using usin Haskell

CSCC24 Functional Programming Scheme Part 2

CS 360: Programming Languages Lecture 10: Introduction to Haskell

Chapter 15. Functional Programming Languages

SMURF Language Reference Manual Serial MUsic Represented as Functions

Shell CSCE 314 TAMU. Haskell Functions

Functional Programming in Haskell for A level teachers

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

List Functions, and Higher-Order Functions

(Func&onal (Programming (in (Scheme)))) Jianguo Lu

Quiz Stuff. Use a full sheet of 8½x11" paper. (Half sheet? Half credit!) ...

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

INTRODUCTION TO HASKELL

Onion Language Reference Manual. By: Andrew Aday, (aza2112) Amol Kapoor (ajk2227), Jonathan Zhang (jz2814)

Organization of Programming Languages CS3200/5200N. Lecture 11

Functional Programming. Big Picture. Design of Programming Languages

The Typed Racket Guide

Java+- Language Reference Manual

Input And Output of C++

Introduction to Functional Programming

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING

Modern Programming Languages. Lecture LISP Programming Language An Introduction

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING

JVM ByteCode Interpreter

Note that pcall can be implemented using futures. That is, instead of. we can use

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Introduction to Functional Programming in Haskell 1 / 56

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

CITS3211 FUNCTIONAL PROGRAMMING. 6. Folding operators

Comp215: More Recursion

Clojure. A (not-so-pure) functional approach to concurrency. Paolo Baldan Linguaggi per il Global Computing AA 2016/2017

HIGHER-ORDER FUNCTIONS

Erlang 101. Google Doc

Haskell: Lists. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, Glenn G.

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Functional Programming. Pure Functional Languages

Lecture 10: Recursion vs Iteration

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

Functional Programming in C++

Topic B: Backtracking and Lists

An Introduction to Scheme

Haskell Review COMP360

8. Control statements

CS 340 Spring 2019 Midterm Exam

Department of Computer Science, University of Otago

Thoughts on Assignment 4 Haskell: Flow of Control

Haskell An Introduction

CMSC 331 Final Exam Section 0201 December 18, 2000

Transcription:

PP 2016/17 Unit 16 Erlang Modules, Functions and Control Structures 1/31 Programming Paradigms Unit 16 Erlang Modules, Functions and Control Structures J. Gamper Free University of Bozen-Bolzano Faculty of Computer Science IDSE

PP 2016/17 Unit 16 Erlang Modules, Functions and Control Structures 2/31 Outline 1 Modules and Functions 2 Control Structures 3 Higher-Order Functions 4 Examples

Modules and Functions PP 2016/17 Unit 16 Erlang Modules, Functions and Control Structures 3/31 Outline 1 Modules and Functions 2 Control Structures 3 Higher-Order Functions 4 Examples

Modules and Functions PP 2016/17 Unit 16 Erlang Modules, Functions and Control Structures 4/31 Functions and Modules Erlang is a functional language, so functions are the main building blocks Functions are defined within modules Function and module names must be atoms Functions must be exported before they can be called from outside the module where they are defined The standard library has a lot of predefined modules, e.g., the lists module to work with lists When calling a function from another module you need a qualified name, i.e., module name and function name separated by a colon > lists:reverse([1,2,3,4]). [4,3,2,1] You ve already seen the module io for printing Hello World! > io:format("hello World!\n"). Hello World! ok

Modules and Functions PP 2016/17 Unit 16 Erlang Modules, Functions and Control Structures 5/31 Creating and using Modules To create your own module with some functions, you need to do the following: Write a source file that contains function definitions and has extension.erl Compile the module, which generates an executable file with extension.beam Use the module Let s do this for a very simple piece of code: A function that returns its input value

Modules and Functions PP 2016/17 Unit 16 Erlang Modules, Functions and Control Structures 6/31 Creating a Module This is what the module file basic.erl looks like: -module(basic). -export([mirror/1]). mirror(anything) -> Anything. The first line defines the name of the module, an atom The second line tells Erlang that the function mirror should be visible outside of the module and has one parameter (that is the meaning of /1) The third line defines the function mirror with one argument The symbol -> separates the function head and the function body Notice the similarity to Haskell functions and Prolog-style rules

Modules and Functions PP 2016/17 Unit 16 Erlang Modules, Functions and Control Structures 7/31 Compiling a Module After starting the Erlang shell from the directory of the code file you can compile it with the command c > c(basic). {ok,basic} This will create an executable file basic.beam

Modules and Functions PP 2016/17 Unit 16 Erlang Modules, Functions and Control Structures 8/31 Using a Module Now you can run the functions defined in the module like this: > basic:mirror(1). 1 > basic:mirror(abc). abc > basic:mirror("string"). "string" > mirror(1). ** exception error: undefined shell command mirror/1 Notice that the parameter Anything was bound to different types in each call (number, atom, string) This means that Erlang uses dynamic typing The function name must be qualified with the module name, otherwise you get an error

Modules and Functions PP 2016/17 Unit 16 Erlang Modules, Functions and Control Structures 9/31 Local Functions A module can define several functions Some functions might be local, i.e., not visible outside -module(double). -export([double/1]). double(x) -> times(x, 2). times(x, N) -> X * N. This module defines 2 functions double can be called from outside the module times is local to the module

Modules and Functions PP 2016/17 Unit 16 Erlang Modules, Functions and Control Structures 10/31 Function Declaration Revisited So far, each function consisted of a single line In general, a function declaration consists of a sequence of function clauses separated by semicolons, and terminated by period (.). Name(Arg11,..., Arg1N) [when GuardSeq1] -> Expr11,..., Expr1M;...; Name(ArgK1,..., ArgKN) [when GuardSeqK] -> ExprK1,..., ExprKM. Each clause represents a different matching possibility (cf. Prolog, Haskell) Clauses are separated by a semicolon (;) A function clause consists of a head and a body, separated by ->. A clause head consists of the function name, an argument list, and an optional guard sequence beginning with the keyword when. A clause body consists of a sequence of expressions separated by comma (,)

Modules and Functions PP 2016/17 Unit 16 Erlang Modules, Functions and Control Structures 11/31 Function with Multiple Matching Possiblities This is a simple example with multiple matching possibilities/clauses -module(matching function). -export([number/1]). number(one) -> 1; number(two) -> 2; number(three) -> 3. You can execute it like this > c(matching function). {ok,matching function} > matching function:number(one). 1 > matching function:number(four). ** exception error: no function clause matching...

Modules and Functions PP 2016/17 Unit 16 Erlang Modules, Functions and Control Structures 12/31 Recursive Functions Similar to Prolog, recursion plays a big role in Erlang Erlang is optimized for tail recursion -module(yam). -export([fac/1]). -export([fib/1]). fac(0) -> 1; fac(n) -> N * fac(n-1). fib(0) -> 1; fib(1) -> 1; fib(n) -> fib(n-1) + fib(n-2).

Modules and Functions PP 2016/17 Unit 16 Erlang Modules, Functions and Control Structures 13/31 Function Evaluation Function evaluation works essentially in the same way as in Prolog The function clauses are scanned sequentially (from top to down) until a clause is found that fulfills the following two conditions: the pattern in the clause head can be successfully matched the guard sequence, if any, is true If such a clause cannot be found, a runtime error occurs If such a clause is found, the corresponding clause body is evaluated: the expressions in the body are evaluated sequentially (from left to right) the value of the last expression is returned fac(2) % match clause 2 2 * fac(1) % match clause 2 1 * fac(0) % match clause 1 1 1 2 2 This function is not safe. What happens if you call, e.g., yam:fac(-3)?

Modules and Functions PP 2016/17 Unit 16 Erlang Modules, Functions and Control Structures 14/31 Functions with Guards A guard (sequence) is an optional condition that can be placed in the head of a function clause They can be used to make functions more robust -module(yam). -export([fac2/1]). fac2(0) -> 1; fac2(n) when N > 0 -> N * fac2(n-1). Now you get the following: > yam:fac2(3). 6 > yam:fac2(-3). ** exception error: no function clause matching yam:fac2(-3) We will see later on how to avoid even the runtime error

Control Structures PP 2016/17 Unit 16 Erlang Modules, Functions and Control Structures 15/31 Outline 1 Modules and Functions 2 Control Structures 3 Higher-Order Functions 4 Examples

Control Structures PP 2016/17 Unit 16 Erlang Modules, Functions and Control Structures 16/31 Control Structures: case Control structures look similar to functions The case statement in Erlang looks like this > Animal = "dog". "dog" > case Animal of > "dog" -> underdog; > "cat" -> thundercat; > "elephant" -> dumbo; > -> something else > end. underdog The case statement uses pattern matching to distinguish various cases A pattern might optionally be followed by a guard The underscore ( ) matches anything The pattern can be more complex, e.g., lists or tuples

Control Structures PP 2016/17 Unit 16 Erlang Modules, Functions and Control Structures 17/31 Control Structures: if In contrast to the case statement, the if statement uses guards i.e., boolean conditions that must be satisfied for a match to succeed > if > X > 0 -> positive; > X < 0 -> negative; > true -> zero > end. One of the branches of a case or if statement has to be true, otherwise a runtime error will occur if no match is found The reason is that case and if are functions that have to return a value In the above example, the last guard is simply set to the expression true true always succeeds alternatively, X == 0 could be used

Higher-Order Functions PP 2016/17 Unit 16 Erlang Modules, Functions and Control Structures 18/31 Outline 1 Modules and Functions 2 Control Structures 3 Higher-Order Functions 4 Examples

Higher-Order Functions PP 2016/17 Unit 16 Erlang Modules, Functions and Control Structures 19/31 Higher-order Functions A function that returns functions or takes functions as arguments is called a higher-order function In Ruby this was achieved passing code blocks In Prolog, you can pass as argument a list that contains functor and arguments of a predicate (which then can be composed to a predicate using the univ operator =..) In Haskell, we have seen functions such as map that take a (anonymous) function and a list and apply the function to the list In Erlang arbitrary functions can be assigned to variables and be passed around like any other data type Let s start with anonymous functions (functions without a name)

Higher-Order Functions PP 2016/17 Unit 16 Erlang Modules, Functions and Control Structures 20/31 Anonymous Functions Let s define a function that negates its argument > Negate = fun(i) -> -I end. #Fun<erl eval.6.111823515> > Negate(1). -1 > Negate(-3). 3 The keyword fun defines an anonymous function that has no name This function is assigned to variable Negate Negate actually is the function, not the value returned by the function By assigning the function to a variable, the function can be passed as a parameter like any other data, and it can even be called with different parameters We will show this next

Higher-Order Functions PP 2016/17 Unit 16 Erlang Modules, Functions and Control Structures 21/31 Lists and Higher-order Functions A large number of higher-order functions are defined for lists in the module lists lists:foreach takes a function and a list as arguments and iterates over the list applying the function to each element > Numbers = [1,2,3,4]. [1,2,3,4] > Print = fun(x) -> io:format("~p~n", [X]) end. > lists:foreach(print,numbers). 1 2 3 4 ok io:format/2 is a function for formated printing ~p pretty prints an argument, ~n prints a newline

Higher-Order Functions PP 2016/17 Unit 16 Erlang Modules, Functions and Control Structures 22/31 Mapping Lists The map function is another function that takes a function and a list as arguments However, it applies the function to each element and builds a new list with the results The following example increases each element of a list by 1 > Numbers = [1,2,3,4]. [1,2,3,4] > lists:map(fun(x) -> X+1 end,numbers). [2,3,4,5] This example shows also that an anonymous function can be defined directly when a function is called

Higher-Order Functions PP 2016/17 Unit 16 Erlang Modules, Functions and Control Structures 23/31 Filtering Lists Lists can also be filtered using a boolean function. The function lists:filter builds a new list with all elements that satisfy a given function > Small = fun(x) -> X < 3 end. > Small(4). false > Small(2). true > lists:filter(small,numbers). [1,2]

Higher-Order Functions PP 2016/17 Unit 16 Erlang Modules, Functions and Control Structures 24/31 Testing Lists The function Small can also be used to test whether all elements (lists:all) of a list are less than 3 with, or any element (lists:any) of a list is less than 3 > lists:all(small, [0,1,2]). true > lists:all(small, [0,1,2,3]). false > lists:any(small, [0,1,2,3]). true > lists:any(small, [3,4,5]). false The two functions applied on empty lists > lists:any(small, []). false > lists:all(small, []). true

Higher-Order Functions PP 2016/17 Unit 16 Erlang Modules, Functions and Control Structures 25/31 Extract and Skip Prefix of Lists You can also use a filter condition to extract a prefix of a list (lists:takewhile), i.e., make a list of all elements at the head of a list that satisfy the filter or to skip that elements (lists:dropwhile) > lists:takewhile(small, [1,2,3,4]). [1,2] > lists:dropwhile(small, [1,2,3,4]). [3,4] > lists:takewhile(small, [1,2,1,4,1]). [1,2,1] > lists:dropwhile(small, [1,2,1,4,1]). [4,1]

Higher-Order Functions PP 2016/17 Unit 16 Erlang Modules, Functions and Control Structures 26/31 Fold Functions on Lists/1 Fold functions are a family of functions that accumulate a return value while processing a data structure, such as a list A fold function takes as argument: a function an initial accumulator value and a list Lets sum up the elements of our list of numbers > Adder = fun(item,sumsofar) -> Item + SumSoFar end. > InitSum = 0. > lists:foldl(adder,initsum,[1,2,3,4]). 10

Higher-Order Functions PP 2016/17 Unit 16 Erlang Modules, Functions and Control Structures 27/31 Fold Functions on Lists/2 Fold functions for lists come in two flavors: foldl and foldr Both of them take the same parameters: a function, initial accumulator value, and a list The difference is the order in which they combine the list elements with the accumulator foldl accumulates elements from left to right foldr accumulates elements from right to left > P = fun(a, AccIn) -> io:format(" p ", [A]), AccIn end. > lists:foldl(p, void, [1,2,3]). > 1 2 3 void > lists:foldr(p, void, [1,2,3]). > 3 2 1 void Notice that the function assigned to variable P has side-effects, hence is not pure functional!

Examples PP 2016/17 Unit 16 Erlang Modules, Functions and Control Structures 28/31 Outline 1 Modules and Functions 2 Control Structures 3 Higher-Order Functions 4 Examples

Examples Example: Length of Lists Let s write a function to compute the length of a list (i.e., length/1) -module(mylists). -export([len/1]). len([]) -> 0; len([ T]) -> 1 + len(t). This is how this function is executed: len([1,2,3]) = len([1 [2,3]]) = 1 + len([2 [3]]) = 1 + 1 + len([3 []]) = 1 + 1 + 1 + len([]) = 1 + 1 + 1 + 0 = 1 + 1 + 1 = 1 + 2 = 3 This is function is not tail-recursive PP 2016/17 Unit 16 Erlang Modules, Functions and Control Structures 29/31

Examples PP 2016/17 Unit 16 Erlang Modules, Functions and Control Structures 30/31 Example: Length of Lists Tail-recursive To come up with a tail-recursive version, we use an accumulator -module(mylists). -export([tail len/1]). tail len(l) -> tail len(l, 0). tail len([], Acc) -> Acc; tail len([ T], Acc) -> tail len(t, Acc+1). This is how this function is executed: tail len([1,2,3]) = tail len([1,2,3], 0) tail len([1 [2,3]], 0) = tail len([2,3], 0+1]) tail len([2 [3], 1]) = tail len([3], 1+1]) tail len([3 []], 2]) = tail len([], 2+1]) tail len([],3]) = 3 This function is tail-recursive

Examples PP 2016/17 Unit 16 Erlang Modules, Functions and Control Structures 31/31 Example: Reverse a List We write a function reverse/1 to reverse a list -module(mylists). -export([reverse/1]). reverse(list) -> reverse(list, []). reverse([h T], Acc) -> reverse(t, [H Acc]); reverse([], Acc) -> Acc. This is how this function is executed: reverse([1,2,3]) = reverse([1,2,3], []) reverse([1 [2,3]], []) = reverse([2,3], [1 []]) reverse([2 [3]], [1]) = reverse([3], [2 [1]]) reverse([3 []], [2,1]) = reverse([], [3 [2,1]]) reverse([], [3,2,1]) = [3,2,1]