Sml Tutorial4

download Sml Tutorial4

of 2

Transcript of Sml Tutorial4

  • 8/6/2019 Sml Tutorial4

    1/2

    sml_tutorial4.sml Page 1

    datatype 'label btree =Empty|Nodeof 'label * 'label btree *'label btree;

    fun lower (nil)= nil | lower(c::cs)=(Char.toLower c)::lower(cs)val[#"s", #"a", #"n"]= lower [#"S", #"a", #"N"]

    fun strLT(x,y)=

    implode(lower(explode x)) < implode(lower(explode y));valtrue= strLT ("Cupertino","San Diego")valfalse= strLT ("Frank","Alice in the wonderland")

    fun insert lt Empty x =Node(x, Empty, Empty) | insert lt (T as Node(y, left, right)) x =

    if lt(x,y)thenNode(y, (insert lt left x), right) elseif lt(y, x)thenNode(y, left, (insert lt right x)) else(* x=y *) T;

    val t =Node("ML", Node("as", Node("a",Empty,Empty), Node("in",Empty,Empty)

    ), Node("types",Empty,Empty) );insert strLT t "function";

    exceptionEmptyTree;

    (* deletemin(T) returns a pair consisting of the least

    element y in tree T and the tree that results if we

    delete y from T. It is an error if T is empty *)

    fun deletemin(Empty)=raiseEmptyTree | deletemin(Node(y, Empty, right))=(y, right)(*if left is empty

    then y is the min element *) | deletemin(Node(w, left, right))=

    let val(y, L)= deletemin(left) in (y, Node(w, L, right)) end;

    (* delete *)

    fun delete lt Empty x =Empty | delete lt (Node(y, left, right)) x = if lt(x,y)thenNode(y, (delete lt left x), right) elseif lt(y,x)thenNode(y, left, (delete lt right x)) else(* x=y *) case(left, right)of (Empty, r)=> r |(l, Empty)=> l |(l, r)=> let val(z, r1)= deletemin(r) in Node(z, l, r1) end;

    (* A polymorphic recursive datatype to represent trees containing any

    * type of values at the leaves.

    * *)

    datatype 'a Tree=Leafof 'a |Nodeof 'a Tree* 'a Tree

  • 8/6/2019 Sml Tutorial4

    2/2

    sml_tutorial4.sml Page 2

    val iTree =Node(Leaf(1), Leaf(2))val sTree =Node(Leaf("A"), Node(Leaf("B"), Leaf("C")));

    (* flatten takes 'a Tree and returns 'a list *)

    fun flatten (Leaf x)=[x] | flatten (Node(l, r))= flatten(l) @ flatten(r);flatten sTree;

    datatypePayment=Cashofreal|Checkofstring*int*real;(* amount : Payment -> double *)fun amount (Cash x)= x | amount (Check(bank, num, x))= x;

    fun isCheck (Cash(_))=false | isCheck(Check(_,_,_))=true;

    (* tally: Payment list -> real

    * sum the amounts of all payments in a list *)

    fun tally nil =0.0 | tally(p::ps)= amount(p) + tally(ps);val p1 =Cash(100.0);val p2 =Check("Wells Fargo", 1001, 55.60);val p3 =Check("City Bank", 2001, 35.40);

    amount(p3);tally [p1, p2, p3];

    (* giving a value that can replace "???" and cause the whole thing

    * evaluate to integer 42*)

    fun xzardoz(x:string*string):int=caseInt.fromString((#2 x)^(#1 x))of

    NONE=> 41 |SOME n => 2*n;

    (* zardoz(???)

    * replace with xzardoz(???) *)

    (* val 42 = xzardoz("1", ???) ; *)

    fun yzardoz (x:string, (y:(int*int)option, z:int)):int=case y of SOME(7,x)=> x*z - 1 |SOME(x,z)=> x*z |NONE=> size(x)*z + 1

    (* yzardoz(???)

    * replace the right 42 with yzardoz(???) *)

    datatype sa =Doneofint|Functionof sa->int fun zardoz(f:sa):int=

    case f ofDone(n)=> n

    |Function(g)=> g(f);