FoCS 202324 8
FoCS 202324 8
n =>n n->
(fn(fun *2)n 17;
* 2) 17;;
> val it = 34
- : int = 34 : int
Functions
Functionswithout
withoutNames
Names
In both cases:
In : double 2;
Out: - : int = 4
Functions
Functionswithout
withoutNames
Names
for example:
let is_zero = fun x -> match x with 0 -> true | _ -> false
k n
In : it 1 3;
Out: - : int = 7
CurriedFunctions
Curried Functions
Curried Functions
Curried Functions
A curried function returns another function as its result.
A curried function returns another function as its result.
val prefix = (fn a => (fn b => a^b));
let
val prefix
prefix =
= = (fun
(fn a -> (fun
a string
=> (fn -> b
b => -> a ^ b))
a^b));-> string)
803 > val prefix fn: (string
val prefix : string -> string -> string = <fun>
803 > val prefix = fn: string -> (string -> string)
prefix yields functions of type string -> string.
prefix yields functions of type string -> string.
val promote = prefix "Professor ";
> let
val promote
promote
val = ==
promote prefix
prefix "Professor
"Professor
fn: string "; ";;
-> string
>val
val promote
promote promote
"Mopp";:= string -> string
fn: string = <fun>
-> string
promote
> "Mopp";
"Professor Mopp" : string
promote "Mopp";;
> "Professor Mopp" : string
- : string = "Professor Mopp"
Shorthand
Shorthandfor
forCurried
CurriedFunctions
Functions
A function-returning function is just a function of two arguments
let prefix
A function-returning a bis=just
function a a^function
b;; of two arguments.
val
This curried prefix
function : string
syntax -> (nested
is nicer than string fn-> string)
binders:
de 804
Syntax: the symbol -> associates to the right
fun prefix a b = a^b;
> val prefix = ... as before
fun x1 x2 … xn -> E let f x1 x2 … xn = E
val dublet
= prefix
dub "Sir ";
= prefix “Lady ";;
val
> val dub dub : string
= fn: string ->
->string
string= <fun>
val insort : ('a -> 'a -> bool) -> ('a list -> 'a list )
IN OUT
Partial
PartialApplication:
Application:AACurried
CurriedInsertion
InsertionSort
Sort
⎛ ⎞
⎛ ⎞T a d
a b c ⎜ ⎟
⎝ ⎠ = ⎜b e⎟
⎝ ⎠
d e f
807 c f
fun hd (x::_) = x;
fun tl (_::xs) = xs;
let rec transp = function
| [] ([]::_)
fun transp :: _ -> []
= []
| rows
| transp -> (map
rows List.hd
= (map hd rows)
rows)::::
(transp (map List.tl rows))
(transp (map tl rows))
Example:
Example:Matrix
MatrixTranspose
Transpose
In : List.hd;;
Out: - : 'a list -> ‘a = <fun>
In : transp;
Out: - : 'a list list -> 'a list list
⎛ ⎞
B1
! " ⎜ ⎟ ! "
⎜ .. ⎟
A1 ··· A k · ⎜ . ⎟ = A 1 B1 + · · · + A k Bk
⎝ ⎠
Bk
1 1 5 −1 2
(2, 0) · (1,(i,j)
For element 4) =of2×A 1x +B:0 × 4 = 2.
Coding matrixdot-product
multiplication of
in arow i and column
conventional j
programming language usua
lves three nested loops. It is hard to avoid mistakes in the subscripting, wh
n runs slowly due to redundant internal calculations.
Matrix Multiplication
Matrix in
Multiplication OCaml
in ML
Matrix Multiplication in ML
inner map
x =
outer map
x =
List
ListFunctionals
Functionalsfor
forPredicates
Predicates
List Functionals for Predicates
Dual to exists:
let rec all p = function
| [] -> true
| x::xs -> (p x) && all p xs
val all : ('a -> bool) -> 'a list -> bool = <fun>
Example:
> exists (fun x -> x mod 2 = 0) [1; 2; 3];;
- : bool = true
let inter xs ys =
Slide 811 fun inter(xs,ys) =
filter (fun x -> member x ys) xs;;
filter (fn x => member(x,ys)) xs;
fun disjoint(xs,ys) =
let disjoint xs ys =
allall (fnx x->=>all
(fun all(fun
(fn yy ->
=> xx<>y)
<> y)ys)
ys)xs;
xs
> val disjoint = fn: ''a list * ''a list -> bool