Functions in SML - Kansas State University
Defining Functions
Defining values of simple types
? val i = 3;
val i = 3 : i n t
Defining function values:
? v a l i n c = f n ( x ) => x + 1 ;
v a l i n c = f n : i n t ?> i n t
? inc (3);
val i t = 4 : i n t
? v a l i s 3 = f n x =>
i f x = 3 then ¡± y e s ¡± e l s e ¡± no ¡± ;
v a l i s 3 = f n : i n t ?> s t r i n g
? i s 3 4;
v a l i t = ¡± no ¡± : s t r i n g
Function types: fn: ->
Functions in SML
Amtoft
from Hatcliff
from Leavens
Defining Functions
Functions as Values
Multiple Arguments
Currying
Polymorphism
Fun with fun
Functions in SML
Amtoft
from Hatcliff
from Leavens
Defining Functions
The previous definitions can be abbreviated:
fun () = ;
? fun i n c ( x ) = x + 1 ;
v a l i n c = f n : i n t ?> i n t
? fun i s 3 x =
i f x = 3 then ¡± y e s ¡± e l s e ¡± no ¡± ;
v a l i s 3 = f n : i n t ?> s t r i n g
? fun t e s t ( x , y ) = i f x < y then y e l s e x +1;
v a l t e s t = f n : i n t ? i n t ?> i n t
Functions as Values
Multiple Arguments
Currying
Polymorphism
ML Programs
A (simple) ML program is generally a sequence of
function definitions
Functions in SML
Amtoft
from Hatcliff
from Leavens
Defining Functions
Functions as Values
fun push ( v a l u e , s t a c k )
...
...;
fun pop ( s t a c k )
...
...;
fun empty ( s t a c k )
...
...;
fun make?s t a c k ( v a l u e )
...
...;
Multiple Arguments
Currying
Polymorphism
Functions as Values
Functions in SML
Amtoft
from Hatcliff
from Leavens
Functions can be anonymous
? f n x => x + 2 ;
v a l i t = f n : i n t ?> i n t
Defining Functions
Functions as Values
Multiple Arguments
Currying
Functions can be tuple components
? v a l p = ( f n ( x , y ) => x + y ,
f n ( x , y ) => x ? y ) ;
v a l p = ( fn , f n ) :
( i n t ? i n t ?> i n t ) ? ( i n t ? i n t ?> i n t )
? #1(p ) ( 2 , 3 ) ;
val i t = 5 : i n t
? #2(p ) ( 2 , 3 ) ;
v a l i t = ?1 : i n t
Polymorphism
Functions in SML
Functions as Values
Amtoft
from Hatcliff
from Leavens
Functions can be list elements
? fun add1 ( x )
v a l add1 = f n
? fun add2 ( x )
v a l add2 = f n
? fun add3 ( x )
v a l add3 = f n
=
:
=
:
=
:
x +
int
x +
int
x +
int
1;
?> i n t
2;
?> i n t
3;
?> i n t
? v a l l s = [ add1 , add2 , add3 ] ;
v a l l s = [ fn , fn , f n ] : ( i n t ?> i n t ) l i s t
? hd ( l s ) ( 3 ) ;
val i t = 4 : i n t
? hd ( t l ( l s ) ) ( 3 ) ;
val i t = 5 : i n t
Defining Functions
Functions as Values
Multiple Arguments
Currying
Polymorphism
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- functions and call routines wpi
- functions that take functions as arguments university of california
- worksheet data handling using pandas
- reading and writing data with pandas
- pandas udf and python type hint in apache spark 3
- making a fast curry push enter vs eval apply for higher order languages
- functions in sml kansas state university
- pandas for dummies qrious
- python pandas quick guide university of utah
- pandas dataframe notes university of idaho
Related searches
- columbus state university in georgia
- columbus state university in ga
- kansas state obituaries archives
- kansas state archives death records
- healthy kids kansas state employees
- kansas state case search
- kansas state court search
- kansas state directory
- kansas state holiday 2020
- kansas state employee holidays
- kansas state football watch live
- kansas state treasurer unclaimed money