Escolar Documentos
Profissional Documentos
Cultura Documentos
Topics
What is functional language?
Components of ML
Yongyuth Permpoontanalarp
slide 1
Standard ML
Standard ML developed at Edinburgh Univ. at '84-'88
Declarative programming:
Logic programming eg. PROLOG
Functional programming eg. ML
Key features of ML
slide 2
Functional language
Curried functions
Higher order functions
Polymorphic : operations for various types
Abstract data types
Strong typing
No reassignment to variables
After a variable is given a value, then no change
Referential transparency : easy to formal analysis
slide 3
slide 4
Functional Language
Mathematics
Declarative prog.
Functional prog. => Lambda calculus
Logic prog. => Logic
f(input)=output
f(a1,,an)=exp[a1,,an]
Program correctness
Program transformation : partial reduction for efficiency
slide 6
Example
fun double x = 2 * x;
(* definition *)
double 3;
(* application *)
=2*3
=6
fun times4 x = double(double x);
(* defn *)
times4(5) = double(double 5)
(* app *)
= double(2 * 5)
= double(10)
= 2 * 10
= 20
slide 7
slide 8
Types
Strong typing
slide 9
Using ML
slide 10
Variables in ML
5;
val it = 5 : int
abc;
val it = abc : string
it means the previously typed expression
Semicolon must end all expressions
slide 11
slide 12
Operators
Concatenation of strings
Arithmetic operators
Infix: +, , * , /, mod
~ denotes unary minus
Comparison operators
<> means not equal
=, <=, <, >, >= as usual
Logical operators
andalso, orelse, not
~3*(~4);
val it = 12 : int
foo ^ bar;
val it = foobar : string
4 <= 3;
val it = false : bool
love < war;
val it = true : bool
slide 13
If-then-else expression
slide 14
Types
slide 15
slide 16
Types (cont.)
Type determination
ML will determine the type for most expressions using
clues such as the types of arguments
#Z < #a;
val it=true:bool
abc <= ab;
val it=false:bool
Integers, reals
slide 17
slide 18
Type determination
Type determination
f(value)
Input
fun f(input:type)
Output
fun f(input:type1):type2 = exp
slide 19
slide 20
ML identifiers
$, ?, &, @,
At most, 20 characters
Variables can hold not only data but also functions
slide 21
slide 22
Tuples
slide 23
slide 24
Lists
Operators on lists
hd([1,2]);
hd [];
val it = 1 : int
error
tl([1,2]);
tl [];
val it = [2] : int list
error
tl [1];
val it = [] : int list
Parenthesis is not needed for arguments of oneargument function
slide 25
Operators on lists
slide 26
Constructors
slide 27
slide 28
Function definition
1.
2.
3.
4.
Function application
FE
Keyword fun
Function name and formal parameters
=
Expression giving the value returned by the function
Function application
slide 30
Patterns
slide 31
slide 32
Example of pattern
1.
2.
Keyword fun
One or more expressions of the form
pattern = expression, separated by |
fun pattern1=expression1 |
pattern2=expression2 |
patternn=expressionn
slide 33
Local environments
slide 34
expression
slide 35
slide 36
Exception of function
Exception of function
slide 37
How to do Exception
slide 38
Handling exception
Define it
exception EmptyList;
EmptyList is the name of an exception
Catch exceptions
<expression> handle <match>
Expression = function definition that we want to handle
exceptions
slide 39
slide 40
Example
Statement lists
fun foo(i) = (
print(The value );
print(i:int);
print( was given\n) );
val foo = fn : int unit
slide 41
Curried functions
slide 42
Curried functions
Normal function : takes one argument as a tuple
Curried function : takes more than one parameter
(* normal function *)
(* curried function *)
slide 44
Curried functions
Polymorphic functions
Eg. Stacks
No restriction on types of data ie. a list
Code sharing or generic function
slide 45
slide 46
Example
identity(2);
val it=2:int
identity(hello);
val it=hello:string
slide 47
slide 48
Example
Example
fun >>((a:int,b),(c,d)):bool=
if a>c orelse (a=c andalso b>d) then true
else false;
>>((2,3),(1,4));
val it = true : bool
ins >> ((3,3),[(1,3),(2,4),(5,4)]);
val it = [(1,3),(2,4),(3,3),(5,4)] : (int * int) list
slide 49
slide 50
Example
Example
Program 6: isort c l1 = l2
To sort list l1 of any type, c=comparison operator,
l2=output sorted list
slide 52
Example
Higher-order functions
higher-order functions
Skeleton of computation
Three higher-order functions
Specialization = non-polymorphic
(bound with some specific data)
map
reduce
filter
slide 53
Map
slide 54
slide 55
slide 56
Reduce
slide 57
slide 58
Conclusion
slide 59
Declarative style
Curried function
Polymorphism
Higher-order function
slide 60