COMP251: Functional Programming with ML
HKUST
(HKUST) ML 1 / 58
COMP251: Functional Programming with ML HKUST (HKUST) ML 1 / 58 - - PowerPoint PPT Presentation
COMP251: Functional Programming with ML HKUST (HKUST) ML 1 / 58 Introduction Functional Programming (FP) A program implements a mapping from input values to output values. In imperative programming, this mapping is achieved indirectly by
(HKUST) ML 1 / 58
Introduction
(HKUST) ML 2 / 58
Introduction
(HKUST) ML 3 / 58
Introduction
(HKUST) ML 4 / 58
Introduction
◮ Scheme is weakly typed, ML is strongly typed. ◮ In Scheme, there is no syntactic difference between programs and data:
◮ We choose ML because it is small. ◮ Mastering one FP language, the others are easy to learn. (HKUST) ML 5 / 58
Introduction
(HKUST) ML 6 / 58
Introduction
ML 7 / 58
Basic types, values, and expressions
(HKUST) ML 8 / 58
Basic types, values, and expressions
◮ whenever an expression has no interesting value. ◮ when a function is to have no arguments.
(HKUST) ML 9 / 58
Basic types, values, and expressions
(HKUST) ML 10 / 58
Basic types, values, and expressions
(HKUST) ML 11 / 58
Basic types, values, and expressions
(HKUST) ML 12 / 58
Basic types, values, and expressions
(HKUST) ML 13 / 58
Basic types, values, and expressions
(HKUST) ML 14 / 58
Basic types, values, and expressions
(HKUST) ML 15 / 58
Basic types, values, and expressions
(HKUST) ML 16 / 58
Basic types, values, and expressions
(HKUST) ML 17 / 58
Identifiers, bindings, and declaration
1Will be covered in the grammar section. (HKUST) ML 18 / 58
Identifiers, bindings, and declaration
(HKUST) ML 19 / 58
Identifiers, bindings, and declaration
(HKUST) ML 20 / 58
Identifiers, bindings, and declaration
(HKUST) ML 21 / 58
Identifiers, bindings, and declaration
(HKUST) ML 22 / 58
Identifiers, bindings, and declaration
(HKUST) ML 23 / 58
Pattern Matching
(HKUST) ML 24 / 58
Pattern Matching
(HKUST) ML 25 / 58
Pattern Matching
(HKUST) ML 26 / 58
Functions
(HKUST) ML 27 / 58
Functions
(HKUST) ML 28 / 58
Functions
(HKUST) ML 29 / 58
Functions
(HKUST) ML 30 / 58
Functions
(HKUST) ML 31 / 58
Functions How Does ML Deduce the Type?
(HKUST) ML 32 / 58
Functions How Does ML Deduce the Type?
◮ The types of arithmetic operators are built-in, and no coercion is done. ◮ In a conditional expression, the expression itself and the sub-expressions
◮ The return type of a function is the same as the type of the expression
(HKUST) ML 33 / 58
Functions How Does ML Deduce the Type?
(HKUST) ML 34 / 58
Functions Higher-Order Functions
(HKUST) ML 35 / 58
Functions Higher-Order Functions
(HKUST) ML 36 / 58
Functions Higher-Order Functions
(HKUST) ML 37 / 58
Functions Higher-Order Functions
(HKUST) ML 38 / 58
Functions List Functions
◮ [ ] or nil ◮ head::tail
(HKUST) ML 39 / 58
Functions List Functions
◮ Type of list: ’a list ◮ Type of f: ’a → ’b ◮ Type of map: (’a → ’b) → ’a list → ’b list (HKUST) ML 40 / 58
Functions List Functions
(HKUST) ML 41 / 58
Functions List Functions
(HKUST) ML 42 / 58
Functions List Functions
(HKUST) ML 43 / 58
Functions List Functions
(HKUST) ML 44 / 58
Functions Anonymous Functions
(HKUST) ML 45 / 58
Functions Anonymous Functions
(HKUST) ML 46 / 58
Functions Creating New Infix Operators
(HKUST) ML 47 / 58
Functions Creating New Infix Operators
(HKUST) ML 48 / 58
Defining New Datatypes
(HKUST) ML 49 / 58
Defining New Datatypes
(HKUST) ML 50 / 58
Defining New Datatypes
(HKUST) ML 51 / 58
Defining New Datatypes
(HKUST) ML 52 / 58
Assignments and Environments
(HKUST) ML 53 / 58
Assignments and Environments
(HKUST) ML 54 / 58
Assignments and Environments
(HKUST) ML 55 / 58
Assignments and Environments
(HKUST) ML 56 / 58
Exceptions
(HKUST) ML 57 / 58
References
(HKUST) ML 58 / 58