sa st Gssa ML - PowerPoint PPT Presentation

1 / 45
About This Presentation
Title:

sa st Gssa ML

Description:

? p??sta?t???? p????a at?s ?? ep??e?t???e? st? p?? ?a ???p???s?? e ta s?stat??? ... uncaught exception Match: [nonexhaustive. match failure] raised at ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 46
Provided by: kostiss
Category:
Tags: gssa | uncaught

less

Transcript and Presenter's Notes

Title: sa st Gssa ML


1
??sa???? st? G??ssa ML
  • Juan Miró
  • ??st?? Sa???a? ltkostis_at_cs.ntua.grgt

2
S??a?t?s?a??? ?a? ???sta?t???? ?????aµµat?sµ??
  • ??a? t??p?? d?a????sµ??
  • ? p??sta?t???? p????aµµat?sµ?? ep??e?t???e? st?
    p?? ?a ???p???s??µe ta s?stat??? t?? p????aµµat??
    µa?
  • ? s??a?t?s?a??? p????aµµat?sµ?? ep??e?t???e? st?
    t? s?stat??? ?a p??pe? ?a ??e? t? p????aµµ? µa?
  • S??a?t?s?a??? p????aµµat?sµ??
  • ?as??eta? st? µa??µat??? µ??t??? t?? ?-????sµ??
    (Church)
  • ?????aµµat?sµ?? ????? µetaß??t??
  • ???a? ap? t? f?s? t?? ??µ???, s??t?µ?? ?a? saf??
    t??p?? p????aµµat?sµ??, st?? ?p??? ap?fe????ta?
    te?e??? ??p???? e?d??? p????aµµat?st??? sf??µata
  • Te??e?ta? ap? p?????? ?? a??te??? t??p??
    p????aµµat?sµ??

3
??af??e?a a?af???? (referential transparency)
  • Se µ?a ???ssa s??a?t?s?a??? p????aµµat?sµ??,
    ? ap?t?µ?s? µ?a? s????t?s?? d??e? p??ta t?
    ?d?? ap?t??esµa ??a t?? ?d?e? t?µ?? t??
    pa?aµ?t??? t??
  • ? s?µa?t??? a?t? ?d??t?ta de? ?s??e? ?at? a?????
    st?? ???sse? p??sta?t???? p????aµµat?sµ??
  • St?? p??sta?t??? p????aµµat?sµ? a?t? s?µßa??e?
    ????
  • ?etaß??t?? p?? ??????ta? ?a? a??????? t?µ?? e?t??
    t?? s?µat?? t?? s????t?s?? (global variables)
  • ????t?s?? ap? t?? ?at?stas? (state) t??
    ?p?????sµ??
  • ????? pa?e?e??e??? (side-effects) p?? µp??e? ?a
    ?p?????? st? p????aµµa

4
?a??de??µa se Pascal
program example(output)var flagboolean function
f(nint) intbegin if flag then f n
else f 2n flag not flagend begin
flag true writeln(f(1)f(2))
writeln(f(2)f(1))end
  • ?? t?p??e? t? p????aµµa
  • 5 ?a? µet? 4
  • ?e??e??? d??t? pe??µ????µe ?t?
  • f(1) f(2) f(2) f(1)
  • Sta µa??µat??? ?? s??a?t?se?? e?a?t??ta?
    µ??? ap? ta ???sµat? t???

5
?etaß??t?? ?a? µetaß??t??
  • St?? ?a?d?? t?? p??ß??µat?? e??a? t? ?e????? ?t?
    ? µetaß??t? flag ep??e??e? t?? t?µ? t?? f
  • ??d???te?a, ? s?µpe??f??? ?fe??eta? st?? a???es?
  • flag not flag
  • Se µ?a ???ssa ????? p???ap??? a?a??se??
    µetaß??t?? de? ?p?????? t?t??a p??ß??µata
  • St?? s??a?t?s?a??? ???sse?, ?? µetaß??t?? e??a?
    ???µata ??a s???e???µ??e? t?µ??, de? e??a?
    ???µata ??a s???e???µ??e? ??se?? µ??µ??
  • ?p????µe ?a t?? ?e???s??µe ??? p??? µetaß??t??

6
? ???ssa ML (Meta Language)
  • G??ssa s??a?t?s?a??? p????aµµat?sµ?? µe t?p???
  • S?ed?asµ??? ??a a????ep?d?ast??? ???s?
    (interactive use)
  • S??d???e? ta pa?a??t? st???e?a
  • ?as?sµ??? st? ?-????sµ? ?a? st?? ap?t?µ?s?
    e?f??se??
  • S??a?t?se?? ?????? t???? (higher-order functions)
  • ??t?µat? d?a?e???s? µ??µ?? (µe ???s? s???????
    s???p?d???)
  • ?f???µ????? t?p??? ded?µ???? (abstract data
    types)
  • S?st?µa a????µ?t?? (module system)
  • ??a???se?? (exceptions)
  • Ge????? ???s?? µ? p??sta?t???, µ?
    a?t??e?µe??st?ef?? ???ssa
  • S?et???? ???sse? OCaml, Haskell,

7
G?at? e?et????µe t?? ML
  • ??p?? ?a? a?st??? s?st?µa t?p??
  • Ge???? ??µata ??a stat??? ??a?t? d??aµ????
    e?????? t?? t?p??
  • S?µpe?asµ?? t?p?? (type inference)
  • ????µ??f?sµ?? ?a? ?e????? p????aµµat?sµ??
    (generic programming)
  • ??a?e???s? µ??µ??
  • Stat??? eµß??e?a ?a? d?µ? ?at? µp???
  • ????af?? e?e???p???s?? s??a?t?se?? (function
    activation records) ?a? ???p???s? s??a?t?se??
    ?????? t????
  • ??e???? ?a? d?µ?? ????
  • ??a???se??
  • ??ad??µ? ????? (tail recursion) ?a? s????e?e?
    (continuations)

8
S??t?µ? ?st???a t?? ???ssa? ML
  • Robin Milner (ACM Turing Award)
  • Logic for Computable Functions
  • Stanford 1970-71
  • Edinburgh 1972-1995
  • Cambridge 1996-
  • ?eta???ssa t?? s?st?µat?? LCF
  • ?p?de??? ?e???µ?t?? (theorem
    proving)
  • S?st?µa t?p?? (type system)
  • S??a?t?se?? ?????? t???? (higher-order
    functions)
  • Ta ???s?µ?p???s??µe t?? ???p???s? SML/NJ
    (Standard ML of New Jersey)

9
? ???ssa ML µ?sa ap? pa?ade??µata

sml Standard ML of New Jersey, v110.XX-
42val it 42 int- 2 3val it 5 int
- fun square x x xval square fn int -gt
int- square 5val it 25 int- squareval
it fun int -gt int
10
?as???? t?p?? t?? ML
  • Booleans
  • true, false bool
  • ????a??? ?a? te?est?? t???
  • 0, 1, 2, int
  • , -, , mod, div, (µ??ad?a?? µe???)
  • S?µß???se???? ?a? te?est?? t???
  • "Robin Milner" string
  • (s?????s? s?µß???se????)
  • ????µ?? ????t?? ?p?d?ast???? ?a? te?est?? t???
  • 1.0, 2.2, 3.14159,
  • , -, , /,

?? te?est?? e??a? a??ste?? p??seta???st????, µe
p??te?a??t?te? ,- lt ,/,div,mod lt .
11
? ???ssa ML µ?sa ap? pa?ade??µata

- 1 2val it false bool- 1 ltgt 2 andalso
true ltgt falseval it true bool- true
false orelse 1 lt 2val it true bool-
"Robin" gt "Milner"val it true bool- 2.56 lt
3.14val it true bool- 2.56 3.14stdIn
Error operator and operand dont agree
operator domain Z Z operand
real real
12
?pe?f??t?s? te?est?? (operator overloading)
- 2 3val it 6 int- 2.0 3.0val it
6.0 real- 2.0 42stdIn Error operator and
operand dont agree operator domain real
real operand real int in
expression 2.0 42
  • ? te?est?? (?a? ????? ?p?? ? ) e??a?
    ?pe?f??t?µ????
  • ????? d?af??et??? e?µ??e?a se ?e??? a?e?a??? ?a?
    d?af??et??? se ?e??? a???µ?? ????t?? ?p?d?ast????
  • ? ML de? ???e? a?t?µat? µetat??p? ap? a?e?a????
    se p?a?µat????? a???µ??? (?p?? p.?. ???e? ? C)

13
? ???ssa ML µ?sa ap? pa?ade??µata
- fun max a b if a gt b then a else bval
max fn int -gt int -gt int- max 10 5val it
10 int- max 10 42val it 42 int
  • ???s??te t?? pe??e??? t?p?
  • int -gt int -gt int
  • ??e? ?t? ? max e??a? µ?a s????t?s? p?? pa???e?
    ??a? a???a?? ?a? ep?st??fe? µ?a s????t?s? p??
    pa???e? ??a? a???a?? ?a? ep?st??fe? ??a? a???a??

14
Currying
  • ?? s??a?t?se?? e??a? a?t??e?µe?a p??t?? t???? ta
    ?p??a µp????µe ?a ta d?a?e???st??µe ?p?? ??a ta
    ???a a?t??e?µe?a (p.?. t??? a?e?a????)

Haskell B. Curry
- fun max a b if a gt b then a else bval max
fn int -gt int -gt int- val max_five max
5val max_five fn int -gt int- max_five
10val it 10 int- max_five 1val it 5
int
15
Currying vs. Tuples
  • ?? ?????µe, µp????µe ?a ???s?µ?p???s??µe p?e??de?
    (tuples) ?? ???sµata ? ap?te??sµata s??a?t?se??

- fun max (a,b) if a gt b then a else bval max
fn int int -gt int- max (10,5)val it 10
int- fun reverse (a,b) (b,a)val reverse
fn a b -gt b a- reverse (10,5)val it
(5,10) int int- max (reverse (10,5))val
it 10 int
16
????µ??f?sµ??
  • ? s????t?s? reverse ??e? ??a? e?d?af????ta t?p?
  • ??t? s?µa??e? ?t? µp????µe ?a a?t?st?????µe µ?a
    d??da ?p?? t? p??t? ???sµa e??a? ?p????d?p?te
    t?p?? ?a? t? de?te?? ???sµa ep?s?? e??a?
    ?p????d?p?te t?p??

- fun reverse (a,b) (b,a)val reverse fn
a b -gt b a
- reverse (10,3.14)val it (3.14,10) real
int- reverse ("foo",(1,2))val it
((1,2),"foo") (int int) string
17
??ad??µ?
  • ?pe?d? de? ?p?????? µetaß??t?? µe t?? pa?ad?s?a??
    ?????a, ta p?????µµata ???s?µ?p????? a?ad??µ? ??a
    ?a e?f??s??? epa??????

- fun sum n if n 0 then 0 else sum (n-1)
nval sum fn int -gt int - sum 2val it
3 int - sum 3val it 6 int - sum 4val
it 10 int
18
?e?est?? ???s?? se d??aµ?
  • ?p????µe ep?s?? ?a ???s??µe ????? a???µ?t?????
    te?est?? ?? s??a?t?se??

- fun x y if y 0 then 1 else x (x
(y-1))val fn int int -gt int - 2
2val it 4 int - 2 3val it 8 int -
2 4val it 16 int
19
?pa?a???s?µ?p???s? ap?te?esµ?t??
  • ?pe?d? de? ????µe µetaß??t??, e?µaste
    a?a??asµ???? ?a epa?a??ß??µe e?f??se?? (?a?
    ?p?????sµ???)
  • ??a µ???d?? ??a ?a ??????µe p?? e????a t??
    pa?ap??? s????t?s? e??a? µe ???s? µ?a? ß????t????
    s????t?s??

fun f x g(square(max(x,4))) (if x lt 1
then 1 else g(square(max(x,4))))
fun f1(a,b) b (if a lt 1 then 1 else b) fun f
x f1(x, g(square(max(x,4)))
20
? ??f?as? let
  • ??a? p?? e?????? t??p?? e??a? ? ???sµ?? e???
    t?p???? ???µat?? ??a t?? epa?a???s?µ?p????µe??
    ??f?as?

fun f x let val gg g(square(max(x,4)))
in gg (if x lt 1 then 1 else gg) end
21
? ??f?as? let de? e??a? a???es?
- let val a 5 in (let val a
a 2 in a end, a)
endval it (7,5) int int
22
S???et?? t?p?? ded?µ???? st?? ML
  • ??????µµata p?? epe?e??????ta? µ??? ßa?µ?t?
    ded?µ??a (scalars ????? d?µ?) de? e??a? p???
    ???s?µa
  • ?? s??a?t?s?a??? ???sse? p????aµµat?sµ?? e??a?
    ?t? p??pe? ??a t?? epe?e??as?a s???et?? t?p??
    ded?µ????
  • ????µe ?d? de? p?e??de?, p?? e??a? s???et?? t?p??
    ded?µ???? ??a t?? a?apa??stas? e??? ???sµ????
    a???µ?? a?t??e?µ???? (p??a??? d?af??et???? t?p??)
  • ? ML ??e? ep?s?? ??ste?, p?? e??a? se????
    ?p????d?p?te a???µ?? a?t??e?µ???? t?? ?d??? ?µ??
    t?p??

23
??ste?
  • ?? p?e??de? pe????e???ta? ap? pa?e???se??,
    ?? ??ste? ap? a????e?
  • ? te?est?? _at_ s??e???e? d?? ??ste?

- (1,2) val it (1,2) int int - 1,2val
it 1,2 int list
- 1,2 _at_ 3,4val it 1,2,3,4 int list
24
Cons
  • ?p????µe ?a p??s??s??µe st???e?a st?? a??? µ?a?
    ??sta? µe t?? te?est? (p??f??eta? cons)
  • ? s?????s? d?? ??st?? de? e??a? t? ?d?? µe t?
    ???s?

- 1 2 3 val it 1,2,3 int
list - 0 itval it 0,1,2,3 int list
  • 1,2 3,4stdIn Error operator and
    operand dont agree
  • operator domain int list int list list
  • operand int list int list
  • in expression
  • (1 2 nil) 3 4 nil

25
???e? s??a?t?se?? ??a ??ste?
- null val it true bool - null 1,2
val it false bool - val l 1,2,3,4val l
1,2,3,4 int list - hd lval it 1 int -
tl lval it 2,3,4 int list - length lval
it 4 int - nilval it a list
26
???sµ?? s??a?t?se?? ??a ??ste?
- fun addto (l,v) if null l then nil
else hd l v addto (tl l,v)val addto fn
int list int -gt int list- - - addto
(1,2,3,2)val it 3,4,5 int list - addto
(1,2,3,2)val it 1,0,1 int list
27
???sµ?? s??a?t?se?? ??a ??ste?
- fun map (f, l) if null l then nil
else f (hd l) map (f, tl l)val map fn
(a -gt b) a list -gt b list- - - fun add2
x x 2val add2 fn int -gt int - map
(add2, 10,11,12)val it 12,13,14 int list
28
?????µe? s??a?t?se?? (?-e?f??se??)
- map (fn x gt x 2, 10,11,12) val it
12,13,14 int list
  • ?? p??t? ???sµa t?? pa?ap??? s????t?s?? ???eta?
    ??µda ??f?as? e??a? µ?a s????t?s? ????? ???µa
  • ? te?est?? fun e??a? ?s?d??aµ?? µe µ?a ??µda
    ??f?as?

- val add2 fn x gt x 2val add2 fn int
-gt int - add2 10val it 12 int
29
??ad??µ???? ??µda e?f??se??
  • ??? ?a???µe a?ad??µ??? ??t? t? ?p??? de? ??e?
    ???µa
  • ??? d????µe ??a!

- let val rec f fn x gt if null x
then nil else (hd x 3) f (tl
x) in f end 1,2,3,4val it
4,5,6,7 int list
30
?a???asµa p??t?p?? (pattern matching)
  • ?? s??a?t?se?? p????? f???? ??????ta? µe
    d?af??et???? e?f??se?? ß?s? ??p???? s???????
  • ?? s??a?t?se?? t?? ML de d?af????? ?a? ep?t??p???
    t?? ???sµ? ?at? pe??pt?se?? ?a? t?? ap?f??? t??
    ???s?? if
  • ?µ??, ? ???sµ?? a?? pe??pt?se?? e??a? e?a?s??t??
    ?? p??? t? se??? eµf???s?? t?? s??a?t?s?a???
    p??t?se??

fun map (f,) map (f,l) f (hd l)
map (f,tl l)
fun map (f,l) f (hd l) map (f,tl l) map
(f,)
31
?a??te??? ???sµ?? µ?s? ta????sµat?? p??t?p??
  • ?? p??t?p? _ ta?????e? µe ??a ta a?t??e?µe?a
  • ?? p??t?p? h t ta?????e? µe µ?a ??sta ?a?
    d??e?
  • t? µetaß??t? h µe t?? ?efa?? t?? ??sta? ?a?
  • t? µetaß??t? t µe t?? ???? t?? ??sta?

fun map (_, ) map (f, ht) f h
map (f, t)
32
???s? sta?e??? ?? p??t?pa
- fun is_zero 0 "yes" stdIn Warning match
nonexhaustive 0 gt ... val is_zero fn
int -gt string - is_zero 0 val it "yes"
string
  • ???e sta?e?? e??? t?p?? p?? ?p?st????e? ?s?t?ta
    µp??e? ?a ???s?µ?p????e? ?? p??t?p?
  • ???? de? µp????µe ?a ??????µe
  • fun is_zero 0.0 "yes"

33
?? e?a?t??t??? ta???asµa p??t?p??
  • St? p??????µe?? pa??de??µa, ? t?p?? t?? is_zero
    ?ta? int -gt string, a??? ta?t?????a ?p???e ?
    p??e?d?p???s? Warning match nonexhaustive
  • ??t? s?µa??e? ?t? ? s????t?s? ???st??e µe p??t?pa
    p?? de? e???t??sa? t? ped?? ???sµ?? t??
    s????t?s??
  • ?at? s???pe?a, e??a? d??at? ?a ?p??????
    p??ß??µata ?????? e?t??es??, ?p??

- is_zero 42 uncaught exception Match
nonexhaustive
match failure raised at ...
34
?a???e? ta????sµat?? p??t?p?? st?? ML
  • ?? p??t?p? _ ta?????e? µe ?t?d?p?te
  • ??a µetaß??t? e??a? ??a p??t?p? p?? ta?????e? µe
    ?p??ad?p?te t?µ? ?a? d??e? t? µetaß??t? µe t??
    t?µ?
  • ??a sta?e?? (e??? t?p?? ?s?t?ta?) e??a? ??a
    p??t?p? p?? ta?????e? µ??? µe t? s???e???µ???
    sta?e??
  • ??a p?e??da (x,y,,z)e??a? ??a p??t?p? p??
    ta?????e? µe ???e p?e??da t?? ?d??? µe??????, t??
    ?p??a? ta pe??e??µe?a ta???????? µe t? se??? t???
    µe ta x,y,,z
  • ??a ??sta x,y,,ze??a? ??a p??t?p? p??
    ta?????e? µe ???e ??sta t?? ?d??? µ?????, t??
    ?p??a? ta st???e?a ta???????? µe t? se??? t??? µe
    ta x,y,,z
  • ??a cons ht e??a? ??a p??t?p? p?? ta?????e? µe
    ???e µ? ?e?? ??sta, t?? ?p??a? ? ?efa?? ta?????e?
    µe t? h ?a? ? ???? µe t? t

35
?a??de??µa ???s?? ta????sµat?? p??t?p??
  • ?a?a???t??? µe ???s? if-then-else
  • ?a?a???t??? µe ???s? ta????sµat?? p??t?p??
  • ?a?at???ste ?t? ?p???e? ep??????? sta p??t?pa
  • ? e?t??es? d???µ??e? p??t?pa µe t? se??? p?? a?t?
    eµfa?????ta? (ap? p??? p??? ta ??t?)

fun fact n if n 0 then 1 else n fact
(n-1)
fun fact 0 1 fact n n fact (n-1)
36
???a pa?ade??µata
  • ? pa?a??t? d?µ? e??a? p??? s?????sµ??? se
    a?ad??µ???? s??a?t?se?? p?? epe?e??????ta?
    ??ste? µ?a pe??pt?s? ??a t?? ?e?? ??sta (nil)
    ?a? µ?a pe??pt?s? ??a ?ta? ? ??sta de? e??a? ?e??
    (ht).
  • ?????sµa ???? t?? st???e??? µ?a? ??sta?
  • ????µ?? t?? st???e??? µ?a? ??sta? µe ??p??a
    ?d??t?ta

fun sum nil 0 sum (ht) h sum t
fun ctrue nil 0 ctrue (truet) 1 ctrue
t ctrue (falset) ctrue t
37
??a? pe?????sµ?? ??aµµ??? p??t?pa
  • ?e? ep?t??peta? ? ???s? t?? ?d?a? µetaß??t??
    pe??ss?te?e? ap? µ?a f???? st? ?d?? p??t?p?
  • G?a pa??de??µa, t? pa?a??t? de? ep?t??peta?
  • ??t? a?t?? p??pe? ?a ???s?µ?p????e? t? pa?a??t?

fun f (a,a) for pairs of equal elements f
(a,b) for pairs of unequal elements
fun f (a,b) if (ab) then for pairs of
equal elements else for pairs of
unequal elements
38
S??d?asµ?? p??t?p?? ?a? let
fun halve nil (nil, nil) halve a (a,
nil) halve (abcs) let val
(x, y) halve cs in (ax, by)
end
  • ?e t? ???s? p??t?p?? st??? ???sµ??? e??? let,
    µp????µe ?a ap?s????s??µe e????a ??a ap?t??esµa
  • ? pa?ap??? s????t?s? pa???e? ?? ???sµa µ?a ??sta
    ?a? ep?st??fe? ??a ?e???? ap? ??ste?, ? ???e µ?a
    ap? t?? ?p??e? ??e? ta µ?s? st???e?a t?? a??????
    ??sta?

39
???s? t?? s????t?s?? halve
- fun halve nil (nil, nil) halve a
(a, nil) halve (abcs) let
val (x, y) halve cs in
(ax, by) end val halve fn 'a
list -gt 'a list 'a list - halve 1 val it
(1,) int list int list - halve 1,2 val
it (1,2) int list int list - halve
1,2,3,4,5,6 val it (1,3,5,2,4,6) int
list int list
40
??a µe?a??te?? pa??de??µa Merge Sort
  • ? s????t?s? halve d?a?e?µe? ta st???e?a µ?a?
    ??sta? se d?? pe??p?? ?sa ??µµ?t?a
  • ???a? t? p??t? ß?µa ??a ta????µ?s? s?????e?s??
  • ? s????t?s? merge s?????e?e? d?? ta????µ?µ??e?
    ??ste?

- fun merge (nil, ys) ys merge (xs, nil)
xs merge (xxs, yys) if (x lt
y) then x merge (xs, yys)
else y merge (xxs, ys) val merge fn int
list int list -gt int list - merge
(2,1,3) val it 1,2,3 int list - merge
(1,3,4,7,8,2,3,5,6,10) val it
1,2,3,3,4,5,6,7,8,10 int list
41
? s????t?s? Merge Sort
fun mergeSort nil nil mergeSort a a
mergeSort theList let val (x,
y) halve theList in merge
(mergeSort x, mergeSort y) end
  • ? t?p?? t?? pa?ap??? s????t?s?? e??a?
  • int list -gt int list
  • ???? t?? t?p?? t?? s????t?s?? merge

42
?a??de??µa ???s?? t?? Merge Sort
- fun mergeSort nil nil mergeSort a
a mergeSort theList let
val (x, y) halve theList in
merge(mergeSort x, mergeSort y) end val
mergeSort fn int list -gt int list - mergeSort
4,3,2,1 val it 1,2,3,4 int list -
mergeSort 4,2,3,1,5,3,6 val it
1,2,3,3,4,5,6 int list
43
F???asµ???? ???sµ?? s??a?t?se??
  • ?p????µe ?a ???s??µe t?p???? s??a?t?se??, a???ß??
    ?p?? ??????µe t?p???? µetaß??t??, µe ???s? let
  • S?????? a?t? ???eta? ??a ß????t???? s??a?t?se??
    p?? de ?e?????ta? ???s?µe? ap? µ??e? t???
  • ?e a?t?? t?? t??p? µp????µe ?a ??????µe t??
    s??a?t?se?? halve ?a? merge ap? t? ?p????p?
    p????aµµa
  • ??t? ??e? ?a? t? p?e????t?µa ?t? ?? es?te?????
    s??a?t?se?? µp????? ?a a?af????ta? se µetaß??t??
    t?? e??te????? s??a?t?se??

44
( Sort a list of integers. )fun mergeSort nil
nil mergeSort e e mergeSort
theList let ( From the given
list make a pair of lists (x,y),
where half the elements of the
original are in x and half are in y. )
fun halve nil (nil, nil) halve a
(a, nil) halve (abcs)
let val (x, y) halve
cs in (ax, by)
end ( Merge two sorted
lists of integers into a single sorted
list. ) fun merge (nil, ys) ys
merge (xs, nil) xs merge
(xxs, yys) if (x lt y) then x
merge(xs, yys) else y
merge(xxs, ys) val (x, y) halve
theList in merge(mergeSort x,
mergeSort y) end
45
??a?efa?a??s? t?? ???ssa? ML
  • ?as???? t?p?? t?? ML int, real, bool, char,
    string
  • ?e?est?? , , -, , div, mod, /, , , _at_, lt,
    gt, lt, gt, , ltgt, not, andalso, orelse
  • ?p????? µeta?? d?? if then else
  • ???sµ?? s??a?t?se?? fun, fn gt ?a? t?µ?? val,
    let
  • ?atas?e?? p?e??d?? (x,y,,z)
  • ?atas?e?? ??st?? x,y,,z, , _at_
  • ?atas?e?ast?? t?p?? , list, ?a? -gt
  • ?a???asµa p??t?p??
  • F???asµ??e? s??a?t?se??
Write a Comment
User Comments (0)
About PowerShow.com