Programmazione Avanzata

Similar documents
COMP 201: Principles of Programming

Visual C# Instructor s Manual Table of Contents

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Chapter 2: Using Data

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

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

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Lecture 2: Variables and Operators. AITI Nigeria Summer 2012 University of Lagos.

Functional Programming Languages (FPL)

CMSC 330: Organization of Programming Languages. Rust Basics

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

CS558 Programming Languages

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

Introduction to Programming Using Java (98-388)

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

Recursion and Induction: Haskell; Primitive Data Types; Writing Function Definitions

Accelerating Information Technology Innovation

EMBEDDED SYSTEMS PROGRAMMING Language Basics

The Warhol Language Reference Manual

COMP519 Web Programming Lecture 17: Python (Part 1) Handouts

Chapter 2: Using Data

Lecture 19: Functions, Types and Data Structures in Haskell

CS 320: Concepts of Programming Languages

Programming with Java

Java Notes. 10th ICSE. Saravanan Ganesh

Topic 1: Introduction

EMBEDDED SYSTEMS PROGRAMMING Language Basics

SECTION II: LANGUAGE BASICS

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Introduce C# as Object Oriented programming language. Explain, tokens,

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

Declaration and Memory

Lecture 2: Big-Step Semantics

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

Data Handing in Python

F28PL1 Programming Languages. Lecture 11: Standard ML 1

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

CS112 Lecture: Primitive Types, Operators, Strings

SPARK-PL: Introduction

Java+- Language Reference Manual

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

Lecture 2 Tao Wang 1

The SPL Programming Language Reference Manual

CSE 431S Type Checking. Washington University Spring 2013

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

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

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

Pace University. Fundamental Concepts of CS121 1

Lexical Considerations

Client-Side Web Technologies. JavaScript Part I

JAC444 - Lecture 1. Introduction to Java Programming Language Segment 4. Jordan Anastasiade Java Programming Language Course

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

Full file at

Ex: If you use a program to record sales, you will want to remember data:

Unit 2: Java in the small. Prepared by: Dr. Abdallah Mohamed, AOU-KW

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

Lesson 02 Working with Data Types. MIT 31043: VISUAL PROGRAMMING By: S. Sabraz Nawaz Senior Lecturer in MIT

Understand Computer Storage and Data Types

SML A F unctional Functional Language Language Lecture 19

Mini-ML. CS 502 Lecture 2 8/28/08

CE221 Programming in C++ Part 1 Introduction

SMURF Language Reference Manual Serial MUsic Represented as Functions

CS558 Programming Languages

Java enum, casts, and others (Select portions of Chapters 4 & 5)

Com S 541. Programming Languages I

Metaprogramming assignment 3

Programming Languages and Techniques (CIS120)

Haskell 98 in short! CPSC 449 Principles of Programming Languages

Unit 2: Java in the small

Array. Prepared By - Rifat Shahriyar

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

FRAC: Language Reference Manual

Java is an objet-oriented programming language providing features that support

DigiPen Institute of Technology

CS313D: ADVANCED PROGRAMMING LANGUAGE

Zheng-Liang Lu Java Programming 45 / 79

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

10/9/2012. Computers are machines that process data. assignment in C# Primitive Data Types. Creating and Running Your First C# Program

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

Full file at

Chapter 3: Operators, Expressions and Type Conversion

Lesson 02 Working with Data Types MIT 31043, Visual Programming By: S. Sabraz Nawaz

egrapher Language Reference Manual

Operators in java Operator operands.

TYPES, VALUES AND DECLARATIONS

Introduction to Programming EC-105. Lecture 2

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

C++ Quick Guide. Advertisements

Bits, Words, and Integers

CPS 506 Comparative Programming Languages. Programming Language Paradigm

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

3. Java - Language Constructs I

CSCI 2041: Basic OCaml Syntax and Features

ECE 122 Engineering Problem Solving with Java

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

EXPRESSIONS AND ASSIGNMENT CITS1001

Transcription:

Programmazione Avanzata Programmazione Avanzata Corso di Laurea in Informatica (L31) Scuola di Scienze e Tecnologie Programmazione Avanzata 1 / 51

Programming paradigms Programmazione Avanzata Corso di Laurea in Informatica (L31) Scuola di Scienze e Tecnologie Programming paradigms 2 / 51

1954 1960 1965 1970 1975 1980 1985 1990 1995 2000 2001 2002 2003 2004 For more than half of the fifty years computer programmers have been writing code, O Reilly has provided developers with comprehensive, in-depth technical information. We ve kept pace with rapidly changing technologies as new languages have emerged, developed, and matured. Whether you want to learn something new or need answers to tough technical questions, you ll find what you need in O Reilly books and on the O Reilly Network. This timeline includes fifty of the more than 2500 documented programming languages. It is based on an original diagram created by Éric Lévénez (www.levenez.com), augmented with suggestions from O Reilly authors, friends, and conference attendees. For information and discussion on this poster, go to www.oreilly.com/go/languageposter. 1986 1990 1990 1991 1991 1993 1994 1995 1996 1996 1997 1997 2000 2001 2001 2003 2003 2004 2004 O Reilly Media, Inc. O Reilly logo is a registered trademark of O Reilly Media, Inc. All other trademarks are property of their respective owners. part#30417 Programming Languages History of Programming Languages www.oreilly.com Programming paradigms 3 / 51

1954 1960 1965 1970 1975 1980 1985 1990 1995 2000 2001 2002 2003 2004 For more than half of the fifty years computer programmers have been writing code, O Reilly has provided developers with comprehensive, in-depth technical information. We ve kept pace with rapidly changing technologies as new languages have emerged, developed, and matured. Whether you want to learn something new or need answers to tough technical questions, you ll find what you need in O Reilly books and on the O Reilly Network. This timeline includes fifty of the more than 2500 documented programming languages. It is based on an original diagram created by Éric Lévénez (www.levenez.com), augmented with suggestions from O Reilly authors, friends, and conference attendees. For information and discussion on this poster, go to www.oreilly.com/go/languageposter. 1986 1990 1990 1991 1991 1993 1994 1995 1996 1996 1997 1997 2000 2001 2001 2003 2003 2004 2004 O Reilly Media, Inc. O Reilly logo is a registered trademark of O Reilly Media, Inc. All other trademarks are property of their respective owners. part#30417 Programming Languages History of Programming Languages www.oreilly.com How we can classify all these languages? Programming paradigms 3 / 51

Programming paradigms Programming paradigms are a way to classify programming languages based on their features. Programming paradigms 4 / 51

Programming paradigms Programming paradigms are a way to classify programming languages based on their features. A programming paradigm is an approach to programming a computer based on a mathematical theory or a coherent set of principles. Programming paradigms 4 / 51

Programming paradigms Programming paradigms are a way to classify programming languages based on their features. A programming paradigm is an approach to programming a computer based on a mathematical theory or a coherent set of principles. Each paradigm supports a set of concepts that makes it the best for a certain kind of problem. Programming paradigms 4 / 51

Programming paradigms Programming paradigms are a way to classify programming languages based on their features. A programming paradigm is an approach to programming a computer based on a mathematical theory or a coherent set of principles. Each paradigm supports a set of concepts that makes it the best for a certain kind of problem. Solving a programming problem requires choosing the right concepts! Programming paradigms 4 / 51

Programming paradigms ter Van Roy Figure 1. Languages, paradigms, and concepts Programming paradigms 5 / 51

Programming paradigms Common programming paradigms include: imperative/procedural: statements are used to change program s state. Imperative programming focuses on describing how a program operates. Programming paradigms 6 / 51

Programming paradigms Common programming paradigms include: imperative/procedural: statements are used to change program s state. Imperative programming focuses on describing how a program operates. functional: computation is treated as the evaluation of mathematical functions and avoids changing-state and mutable data. Programming paradigms 6 / 51

Programming paradigms Common programming paradigms include: imperative/procedural: statements are used to change program s state. Imperative programming focuses on describing how a program operates. functional: computation is treated as the evaluation of mathematical functions and avoids changing-state and mutable data. declarative/logical: expresses the logic of a computation without describing its control flow. A program consists in a set of sentences in logical form, expressing facts and rules about some problem domain. Programming paradigms 6 / 51

Programming paradigms Common programming paradigms include: imperative/procedural: statements are used to change program s state. Imperative programming focuses on describing how a program operates. functional: computation is treated as the evaluation of mathematical functions and avoids changing-state and mutable data. declarative/logical: expresses the logic of a computation without describing its control flow. A program consists in a set of sentences in logical form, expressing facts and rules about some problem domain. object-oriented: it is based on the concept of objects, which may contain both data, the fields, and code, the methods. Programming paradigms 6 / 51

This lecture... In this lecture... Programming paradigms 7 / 51

This lecture... In this lecture...... we will first introduce basic notions of functional programming... Programming paradigms 7 / 51

This lecture... In this lecture...... we will first introduce basic notions of functional programming...... then some basic notions of declarative programming is provided... Programming paradigms 7 / 51

This lecture... In this lecture...... we will first introduce basic notions of functional programming...... then some basic notions of declarative programming is provided...... after that we focus on object-oriented programming... Programming paradigms 7 / 51

This lecture... In this lecture...... we will first introduce basic notions of functional programming...... then some basic notions of declarative programming is provided...... after that we focus on object-oriented programming...... finally an overview of modern programming languages is provided. Programming paradigms 7 / 51

Programmazione Avanzata Corso di Laurea in Informatica (L31) Scuola di Scienze e Tecnologie 8 / 51

Functional programming Programming in a functional language consists of building definitions end using the computer to evaluate expressions. 9 / 51

Functional programming Programming in a functional language consists of building definitions end using the computer to evaluate expressions. Functions are first-class values and can be assigned to names (variables). 9 / 51

Functional programming Programming in a functional language consists of building definitions end using the computer to evaluate expressions. Functions are first-class values and can be assigned to names (variables). Computations consist in the appropriate compositions of defined functions. 9 / 51

Functional programming Programming in a functional language consists of building definitions end using the computer to evaluate expressions. Functions are first-class values and can be assigned to names (variables). Computations consist in the appropriate compositions of defined functions. We will consider F#, a modern functional language integrated in the.net framework. 9 / 51

F# (pronounced F sharp)...... is a strongly typed programming language; 10 / 51

F# (pronounced F sharp)...... is a strongly typed programming language;... supports multi-paradigms (functional, imperative, and object-oriented); 10 / 51

F# (pronounced F sharp)...... is a strongly typed programming language;... supports multi-paradigms (functional, imperative, and object-oriented);... is used as a cross-platform Common Language Infrastructure (CLI) language; 10 / 51

F# (pronounced F sharp)...... is a strongly typed programming language;... supports multi-paradigms (functional, imperative, and object-oriented);... is used as a cross-platform Common Language Infrastructure (CLI) language;... can generate JavaScript and Graphics Processing Unit (GPU) code. 10 / 51

F# (pronounced F sharp)...... is a strongly typed programming language;... supports multi-paradigms (functional, imperative, and object-oriented);... is used as a cross-platform Common Language Infrastructure (CLI) language;... can generate JavaScript and Graphics Processing Unit (GPU) code. 10 / 51

F# (pronounced F sharp)...... is a strongly typed programming language;... supports multi-paradigms (functional, imperative, and object-oriented);... is used as a cross-platform Common Language Infrastructure (CLI) language;... can generate JavaScript and Graphics Processing Unit (GPU) code. Here we will main consider the functional aspects! 10 / 51

Primitive Types (1/2) bool, Boolean values (true or false). byte, Unsigned byte (from 0 to 2 8 1). sbyte, Signed byte (from 2 7 to 2 7 1). int16, 16-bit integer (from 2 15 to 2 15 1). uint16, 16-bit integer (from 0 to 2 16 1). int, 32-bit integer (from 2 31 to 2 31 1). uint32, 32-bit unsigned (from 0 to 2 32 1). int64, 64-bit integer (from 2 63 to 2 63 1). uint64, 64-bit unsigned int (from 0 to 2 64 1). char, Unicode character values. string, Unicode text. decimal, Floating point data type that has at least 28 significant digits. 11 / 51

Primitive Types (2/2) unit, Indicates the absence of an actual value. void, Indicates no type or value. float32, A 32-bit floating point type. float, A 64-bit floating point type. 12 / 51

Values (1/2) bool: true, false. byte, an integer with postfix y (86y). sbyte, an integer with postfix uy (86uy). int16, an integer with postfix s (86s). uint16, an integer with postfix us (86us). int, an integer with the optional postfix l (86 or 86l). uint32, an integer with postfix u or ul (86u or ul). int64, an integer with postfix L (86L). uint64, an integer with postfix UL (86UL). char, a single symbol surrounded by single quotes ( a ). 13 / 51

Values (2/2) string, can be:... a sequence of characters surrounded by double quotes ( Hello\n\n World! );... a sequence of characters surrounded by double quotes and prefixed with @ (@ Hello\n\n World! );... a portion of text (possibly on multiple lines) surrounded by H e l l o World! decimal, a floating point value postfixed with M (0.35M). unit, the value (). float32, a floating point postfixed with f or F (0.35f or 035F). float, a floating point in decimal or exponential form (0.35 or 3.5E 1). 14 / 51

Basic concepts Basic construct in F# is let that can be used to associate a name with a value l e t num = 10 l e t s t r = F# 15 / 51

Basic concepts Basic construct in F# is let that can be used to associate a name with a value l e t num = 10 l e t s t r = F# Each name has a type that is inferred from the associated expression! 15 / 51

Basic concepts Basic construct in F# is let that can be used to associate a name with a value l e t num = 10 l e t s t r = F# Each name has a type that is inferred from the associated expression! Above: num has type int; str has type string ; 15 / 51

Operators Arithmetic Operators: +,,, /, %, ; Comparison Operators: =, <, <=, >, >=, <>; Boolean Operators: not,, &&; Bitwise Operators: &&&,, ˆˆˆ,, <<<, >>>; 16 / 51

Operators Arithmetic Operators: +,,, /, %, ; Comparison Operators: =, <, <=, >, >=, <>; Boolean Operators: not,, &&; Bitwise Operators: &&&,, ˆˆˆ,, <<<, >>>; Arithmetic and Comparison operators are overloaded: the exact type depends on the type of their argument! 16 / 51

Operators Arithmetic Operators: +,,, /, %, ; Comparison Operators: =, <, <=, >, >=, <>; Boolean Operators: not,, &&; Bitwise Operators: &&&,, ˆˆˆ,, <<<, >>>; Arithmetic and Comparison operators are overloaded: the exact type depends on the type of their argument! Differently from Java, no implicit cast is done! 16 / 51

Simple type errors! l e t x = 86u // x has t y p e ubyte l e t y = 86 // y has t y p e i n t l e t z = x+y // This i s an e r r o r!!!! 17 / 51

Basic concepts Functions are first-class values and can be associated with names as any other built-in types: 18 / 51

Basic concepts Functions are first-class values and can be associated with names as any other built-in types: l e t f 1 = fun x > x+1 l e t f 2 ( x ) = x+1 18 / 51

Basic concepts Functions are first-class values and can be associated with names as any other built-in types: l e t f 1 = fun x > x+1 l e t f 2 ( x ) = x+1 Functions can be passed as arguments of other functions: l e t f 3 ( x, f ) = f ( x+2)+1 l e t y = f 3 ( 1, f ) 18 / 51

Basic concepts Functions are first-class values and can be associated with names as any other built-in types: l e t f 1 = fun x > x+1 l e t f 2 ( x ) = x+1 Functions can be passed as arguments of other functions: l e t f 3 ( x, f ) = f ( x+2)+1 l e t y = f 3 ( 1, f ) The type of parameters and the type of returned value can be omitted 18 / 51

Basic concepts Functions are first-class values and can be associated with names as any other built-in types: l e t f 1 = fun x > x+1 l e t f 2 ( x ) = x+1 Functions can be passed as arguments of other functions: l e t f 3 ( x, f ) = f ( x+2)+1 l e t y = f 3 ( 1, f ) The type of parameters and the type of returned value can be omittedwhen they can be inferred from the code! 18 / 51

Basic concepts Functions are first-class values and can be associated with names as any other built-in types: l e t f 1 = fun x > x+1 l e t f 2 ( x ) = x+1 Functions can be passed as arguments of other functions: l e t f 3 ( x, f ) = f ( x+2)+1 l e t y = f 3 ( 1, f ) The type of parameters and the type of returned value can be omittedwhen they can be inferred from the code! Function types have the form: type1 > type2 18 / 51

Type inference The idea of type inference is that you do not have to specify the types of F# constructs except when the compiler cannot conclusively deduce the type. 19 / 51

Type inference The idea of type inference is that you do not have to specify the types of F# constructs except when the compiler cannot conclusively deduce the type. This does not mean that F# is a dynamically typed language or that values in F# are weakly typed. 19 / 51

Type inference The idea of type inference is that you do not have to specify the types of F# constructs except when the compiler cannot conclusively deduce the type. This does not mean that F# is a dynamically typed language or that values in F# are weakly typed. F#, like almost all functional languages, is statically typed! 19 / 51

Type inference The idea of type inference is that you do not have to specify the types of F# constructs except when the compiler cannot conclusively deduce the type. This does not mean that F# is a dynamically typed language or that values in F# are weakly typed. F#, like almost all functional languages, is statically typed! Type annotations can be used to help the compiler to infer the expected type. 19 / 51

Type inference //No a n n o t a t i o n, i n f e r r e d t y p e : i n t i n t > i n t l e t f ( x, y ) = x+y 20 / 51

Type inference //No a n n o t a t i o n, i n f e r r e d t y p e : i n t i n t > i n t l e t f ( x, y ) = x+y // Parameter x i s a n n o t a t e d as f l o a t, i n f e r r e d t y p e : f l o a t f l o a t > f l o a t l e t f ( x : f l o a t, y ) = x+y 20 / 51

Type inference //No a n n o t a t i o n, i n f e r r e d t y p e : i n t i n t > i n t l e t f ( x, y ) = x+y // Parameter x i s a n n o t a t e d as f l o a t, i n f e r r e d t y p e : f l o a t f l o a t > f l o a t l e t f ( x : f l o a t, y ) = x+y //Name x i s a n n o t a ted as f l o a t, i n f e r r e d t y p e : f l o a t f l o a t > f l o a t l e t f ( x, y ) = ( x : f l o a t )+y 20 / 51

Type inference //No a n n o t a t i o n, i n f e r r e d t y p e : i n t i n t > i n t l e t f ( x, y ) = x+y // Parameter x i s a n n o t a t e d as f l o a t, i n f e r r e d t y p e : f l o a t f l o a t > f l o a t l e t f ( x : f l o a t, y ) = x+y //Name x i s a n n o t a ted as f l o a t, i n f e r r e d t y p e : f l o a t f l o a t > f l o a t l e t f ( x, y ) = ( x : f l o a t )+y // Return t y p e o f f i s f l o a t, // i n f e r r e d t y p e : f l o a t f l o a t > f l o a t l e t f ( x, y ) : f l o a t = x+y 20 / 51

Partial evaluation Let us consider the following functions: l e t f 1 ( x, y ) = x+y l e t f 2 x y = x+y 21 / 51

Partial evaluation Let us consider the following functions: l e t f 1 ( x, y ) = x+y l e t f 2 x y = x+y Function f1 has type: 21 / 51

Partial evaluation Let us consider the following functions: l e t f 1 ( x, y ) = x+y l e t f 2 x y = x+y Function f1 has type: v a l f 1 : x : i n t y : i n t > i n t 21 / 51

Partial evaluation Let us consider the following functions: l e t f 1 ( x, y ) = x+y l e t f 2 x y = x+y Function f1 has type: v a l f 1 : x : i n t y : i n t > i n t Function f2 has type: 21 / 51

Partial evaluation Let us consider the following functions: l e t f 1 ( x, y ) = x+y l e t f 2 x y = x+y Function f1 has type: v a l f 1 : x : i n t y : i n t > i n t Function f2 has type: v a l f 2 : x : i n t > y : i n t > i n t 21 / 51

Partial evaluation Let us consider the following functions: l e t f 1 ( x, y ) = x+y l e t f 2 x y = x+y Function f1 has type: v a l f 1 : x : i n t y : i n t > i n t Function f2 has type: v a l f 2 : x : i n t > y : i n t > i n t Function f2 can be partially evaluate: l e t i n c = f 2 1 21 / 51

Partial evaluation Let us consider the following functions: l e t f 1 ( x, y ) = x+y l e t f 2 x y = x+y Function f1 has type: v a l f 1 : x : i n t y : i n t > i n t Function f2 has type: v a l f 2 : x : i n t > y : i n t > i n t Function f2 can be partially evaluate: l e t i n c = f 2 1 The two approaches are in fact equivalent! The second one is the standard (and more efficient). 21 / 51

Type parameters... Let us consider the following definition: l e t s e l e c t x y z w = i f ( x=y ) then z e l s e w 22 / 51

Type parameters... Let us consider the following definition: l e t s e l e c t x y z w = i f ( x=y ) then z e l s e w The compiler has not info for inferring as type for x, y, z and w. 22 / 51

Type parameters... Let us consider the following definition: l e t s e l e c t x y z w = i f ( x=y ) then z e l s e w The compiler has not info for inferring as type for x, y, z and w. However, we know that: 1. x and y must have the same type (say it a); 22 / 51

Type parameters... Let us consider the following definition: l e t s e l e c t x y z w = i f ( x=y ) then z e l s e w The compiler has not info for inferring as type for x, y, z and w. However, we know that: 1. x and y must have the same type (say it a); 2. z and w must have the same type (say it b); 22 / 51

Type parameters... Let us consider the following definition: l e t s e l e c t x y z w = i f ( x=y ) then z e l s e w The compiler has not info for inferring as type for x, y, z and w. However, we know that: 1. x and y must have the same type (say it a); 2. z and w must have the same type (say it b); 3. values of type a must support equality. 22 / 51

Type parameters... Let us consider the following definition: l e t s e l e c t x y z w = i f ( x=y ) then z e l s e w The compiler has not info for inferring as type for x, y, z and w. However, we know that: 1. x and y must have the same type (say it a); 2. z and w must have the same type (say it b); 3. values of type a must support equality. 22 / 51

Type parameters... Let us consider the following definition: l e t s e l e c t x y z w = i f ( x=y ) then z e l s e w The compiler has not info for inferring as type for x, y, z and w. However, we know that: 1. x and y must have the same type (say it a); 2. z and w must have the same type (say it b); 3. values of type a must support equality. Any type satisfying the expected properties (equality for a) can be used in place of a and b, that can be considered as type parameters! 22 / 51

Type parameters... Let us consider the following definition: l e t s e l e c t x y z w = i f ( x=y ) then z e l s e w The compiler has not info for inferring as type for x, y, z and w. However, we know that: 1. x and y must have the same type (say it a); 2. z and w must have the same type (say it b); 3. values of type a must support equality. Any type satisfying the expected properties (equality for a) can be used in place of a and b, that can be considered as type parameters! The following type is inferred for function select : v a l s e l e c t : x : a > y : a > z : b > w: b > b when a : e q u a l i t y 22 / 51

Recursive functions... In functional programming the use of recursive definition is crucial. 23 / 51

Recursive functions... In functional programming the use of recursive definition is crucial. l e t f i b x = i f x<1 then 1 e l s e ( f i b x 1)+( f i b x 2) 23 / 51

Recursive functions... In functional programming the use of recursive definition is crucial. l e t f i b x = i f x<1 then 1 e l s e ( f i b x 1)+( f i b x 2) This definition is not correct! The symbol fib is not defined when the body of the function is evaluated! 23 / 51

Recursive functions... In functional programming the use of recursive definition is crucial. l e t f i b x = i f x<1 then 1 e l s e ( f i b x 1)+( f i b x 2) This definition is not correct! The symbol fib is not defined when the body of the function is evaluated! l e t r e c f i b ( x ) = // Note h e r e the use o f rec i f x<=2 then 1 e l s e ( f i b x 1)+( f i b x 2) 23 / 51

Tuples... A tuple is a grouping of unnamed but ordered values, possibly of different types. ( element,..., element ) 24 / 51

Tuples... A tuple is a grouping of unnamed but ordered values, possibly of different types. ( element,..., element ) Example: l e t f i b ( x ) = l e t r e c f i b ( x ) = i f x<=2 then ( 1, 1 ) e l s e l e t ( a, b )= f i b ( x 1) i n ( a+b, a ) i n l e t ( a, ) = f i b ( x ) i n a 24 / 51

Lists... A list in F# is an ordered, immutable series of elements of the same type. Lists have type a list. 25 / 51

Lists... A list in F# is an ordered, immutable series of elements of the same type. Lists have type a list. You can define a list by explicitly listing out the elements, separated by semicolons and enclosed in square brackets; l e t l i s t 1 2 3 = [ 1 ; 2 ; 3 ] // Type i n t l i s t l e t e m p t y l i s t = [ ] // Type a l i s t! 25 / 51

Lists... A list in F# is an ordered, immutable series of elements of the same type. Lists have type a list. You can define a list by explicitly listing out the elements, separated by semicolons and enclosed in square brackets; l e t l i s t 1 2 3 = [ 1 ; 2 ; 3 ] // Type i n t l i s t l e t e m p t y l i s t = [ ] // Type a l i s t! You can also define list elements by using a range indicated by integers separated by the range operator.. : l e t l i s t 1 = [ 1.. 1 0 ] 25 / 51

Lists... A list in F# is an ordered, immutable series of elements of the same type. Lists have type a list. You can define a list by explicitly listing out the elements, separated by semicolons and enclosed in square brackets; l e t l i s t 1 2 3 = [ 1 ; 2 ; 3 ] // Type i n t l i s t l e t e m p t y l i s t = [ ] // Type a l i s t! You can also define list elements by using a range indicated by integers separated by the range operator.. : l e t l i s t 1 = [ 1.. 1 0 ] List operations: :: is used to add an element at the beginning of the list: a :: list1 @ is used to concatenate two lists: l1@l2 25 / 51

Pattern matching... The match expression provides branching control that is based on the comparison of an expression with a set of patterns. 26 / 51

Pattern matching... The match expression provides branching control that is based on the comparison of an expression with a set of patterns. // Match e x p r e s s i o n. match t e s t e x p r e s s i o n with p a t t e r n 1 [ when c o n d i t i o n ] > r e s u l t e x p r e s s i o n 1 p a t t e r n 2 [ when c o n d i t i o n ] > r e s u l t e x p r e s s i o n 2... 26 / 51

Pattern matching... The match expression provides branching control that is based on the comparison of an expression with a set of patterns. // Match e x p r e s s i o n. match t e s t e x p r e s s i o n with p a t t e r n 1 [ when c o n d i t i o n ] > r e s u l t e x p r e s s i o n 1 p a t t e r n 2 [ when c o n d i t i o n ] > r e s u l t e x p r e s s i o n 2... Pattern can be used to inspect the structure of a value and bind values to variables: match l s t with [ ] > exp 1 v : : t a i l > exp 2 26 / 51

Pattern matching... The match expression provides branching control that is based on the comparison of an expression with a set of patterns. // Match e x p r e s s i o n. match t e s t e x p r e s s i o n with p a t t e r n 1 [ when c o n d i t i o n ] > r e s u l t e x p r e s s i o n 1 p a t t e r n 2 [ when c o n d i t i o n ] > r e s u l t e x p r e s s i o n 2... Pattern can be used to inspect the structure of a value and bind values to variables: match l s t with [ ] > exp 1 v : : t a i l > exp 2 Conditions are boolean expressions that can be used to limit the selection. 26 / 51

Example: Polynomial evaluation A polynomial in a single indeterminate x is an expression o the form: a n x n + + a 1 x + a 0 27 / 51

Example: Polynomial evaluation A polynomial in a single indeterminate x is an expression o the form: a n x n + + a 1 x + a 0 A polynomial can be represented as the list of its coefficients: l e t p o l y = [ an ;... a1 ; a0 ] 27 / 51

Example: Polynomial evaluation A polynomial in a single indeterminate x is an expression o the form: a n x n + + a 1 x + a 0 A polynomial can be represented as the list of its coefficients: l e t p o l y = [ an ;... a1 ; a0 ] Write a function eval that received in input a list of coefficients and a value x computes the value of the polynomial. 27 / 51

Example: Polynomial evaluation Solution 1: l e t r e c e v a l c l i s t ( x : f l o a t ) = match c l i s t with [ ] > 0. 0 c : : t a i l > c ( x f l o a t ( c l i s t. Length 1) )+( e v a l t a i l x ) 28 / 51

Example: Polynomial evaluation Solution 1: l e t r e c e v a l c l i s t ( x : f l o a t ) = match c l i s t with [ ] > 0. 0 c : : t a i l > c ( x f l o a t ( c l i s t. Length 1) )+( e v a l t a i l x ) Solution 2: l e t e v a l 2 c l i s t ( x : f l o a t ) = l e t r e c e v a l 2 c l i s t v = match c l i s t with [ ] > v c : : t a i l > e v a l 2 t a i l ( v x+c ) i n e v a l 2 c l i s t 0. 0 28 / 51

Option type... The option type is used when an actual value might not exist for a named value or variable. 29 / 51

Option type... The option type is used when an actual value might not exist for a named value or variable. An option has an underlying type and can hold a value of that type, or it might not have a value a o p t i o n 29 / 51

Option type... The option type is used when an actual value might not exist for a named value or variable. An option has an underlying type and can hold a value of that type, or it might not have a value a o p t i o n Find the first element in a list matching a predicate: l e t r e c f i n d F i r s t M a t c h i n g pred l = match l with [ ] > None v : : t a i l > i f ( pred v ) then Some v e l s e f i n d F i r s t M a t c h i n g pred t a i l 29 / 51

Option type... The option type is used when an actual value might not exist for a named value or variable. An option has an underlying type and can hold a value of that type, or it might not have a value a o p t i o n Find the first element in a list matching a predicate: l e t r e c f i n d F i r s t M a t c h i n g pred l = match l with [ ] > None v : : t a i l > i f ( pred v ) then Some v e l s e f i n d F i r s t M a t c h i n g pred t a i l The type of findfirstmatching is: pred : ( a > b o o l ) > l : a l i s t > a o p t i o n 29 / 51

Excercises... Ex. 0: Download and install F# developing environment. See instructions available here: https://docs.microsoft.com/en-us/dotnet/fsharp/get-started/ Ex. 1: Write a function that given in input a and b computes their mcd. Ex. 2: Write a function that given in input n returns true if n is a prime number and false otherwise. Ex. 3: Write a function that given in input an integer n computes the list of its prime factors. 30 / 51