挑战题看不懂题目意思,最后好像也没给挑战题的标准答案。
fun same_string(s1 : string, s2 : string) =
s1 = s2
fun all_except_option(x',xs)=
case xs of
[]=>NONE
| x::xs' =>if same_string(x,x') then SOME(xs')
else case all_except_option(x',xs') of
NONE=>NONE
| SOME i =>SOME(x::i)
fun get_substitutions1(lst,s)=
case lst of
[]=>[]
| x::xs =>case all_except_option(s,x) of
NONE=>get_substitutions1(xs,s)
| SOME i =>i@get_substitutions1(xs,s)
fun get_substitutions2(lst,s)=
let fun f(xs,acc)=
case xs of
[]=>acc
| x::xs' =>case all_except_option(s,x) of
NONE=>f(xs',acc)
| SOME i =>f(xs',acc@i)
in f(lst,[]) end
fun similar_names(strs,name)=
let val {first=f,middle=m,last=l}=name
fun helper(firsts,m,l)=
case firsts of
[]=>[]
| x::xs =>{first=x,middle=m,last=l}::helper(xs,m,l)
in
helper(f::get_substitutions2(strs,f),m,l)
end
datatype suit = Clubs | Diamonds | Hearts | Spades
datatype rank = Jack | Queen | King | Ace | Num of int
type card = suit * rank
exception IllegalMove
datatype color = Red | Black
datatype move = Discard of card | Draw
fun card_color c=
case c of
(Clubs,_)=>Black
| (Spades,_) =>Black
|_ =>Red
fun card_value c=
case c of
(_,Num i)=>i
| (_,Ace) =>11
| _ =>10
fun remove_card (cs,c,e)=
case cs of
[]=>raise e
|x::xs=>if(x=c)then xs else remove_card(xs,c,e)
fun all_same_color cs=
case cs of
[]=>true
|[_] =>true
|head::(neck::xs)=>(card_color(head)=card_color(neck))andalso all_same_color(neck::xs)
fun sum_cards cs=
let fun a(xs,acc)=
case xs of
[]=>acc
| x::xs' =>a(xs',card_value(x)+acc)
in
a(cs,0)
end
fun score(cs,i)=
let val sum=sum_cards(cs)
in let val pre=if sum>i then 3*(sum-i) else (i-sum)
in if all_same_color(cs) then pre div 2 else pre
end
end
fun officiate(cs,ms,i)=
let fun continue(cs,hs,ms,i)=
case ms of
[]=>score(hs,i)
| Draw::ms' =>(case cs of
[]=>score(hs,i)
|x::cs' =>if(sum_cards(x::hs)<i)
then continue(cs',x::hs,ms',i)
else score(x::hs,i))
| (Discard c)::ms' =>continue(cs,remove_card(hs,c,IllegalMove),ms',i)
in continue(cs,[],ms,i) end