华盛顿大学程序设计语言第三周作业二答案

挑战题看不懂题目意思,最后好像也没给挑战题的标准答案。

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Contents Chapter 1 Preliminaries 1 1.1 Reasons for Studying Concepts of Programming Languages 2 1.2 Programming Domains 5 1.3 Language Evaluation Criteria 7 1.4 Influences on Language Design 20 1.5 Language Categories 23 1.6 Language Design Trade-offs 24 1.7 Implementation Methods 25 1.8 Programming Environments 33 Summary • Review Questions • Problem Set 34 Chapter 2 Evolution of the Major Programming Languages 39 2.1 Zuse’s Plankalk&uuml;l 40 2.2 Minimal Hardware Programming: Pseudocodes 43 2.3 The IBM 704 and Fortran 45 2.4 Functional Programming: LISP 52 2.5 The First Step Toward Sophistication: ALGOL 60 57 2.6 Computerizing Business Records: COBOL 63 2.7 The Beginnings of Timesharing: BASIC 68 Interview: Alan Cooper—User Design and Language Design 72 2.8 Everything for Everybody: PL/I 74 2.9 Two Early Dynamic Languages: APL and SNOBOL 78 2.10 The Beginnings of Data Abstraction: SIMULA 67 79 2.11 Orthogonal Design: ALGOL 68 80 2.12 Some Early Descendants of the ALGOLs 82 2.13 Programming Based on Logic: Prolog 90 2.14 History’s Largest Design Effort: Ada 92 2.15 Object-Oriented Programming: Smalltalk 97 2.16 Combining Imperative and Object-Oriented Features: C++ 101 2.17 An Imperative-Based Object-Oriented Language: Java 104 2.18 Scripting Languages: JavaScript, PHP, and Python 108 2.19 A C-Based Language for the New Millennium: C# 112 2.20 Markup/Programming Hybrid Languages 115 Summary • Bibliographic Notes • Review Questions •Problem Set 117 Chapter 3 Describing Syntax and Semantics 123 3.1 Introduction 124 3.2 The General Problem of Describing Syntax 125 3.3 Formal Methods of Describing Syntax 127 3.4 Attribute Grammars 141 History Note 142 3.5 Describing the Meanings of Programs: Dynamic Semantics 148 History Note 164 Summary • Bibliographic Notes • Review Questions • Problem Set • Programming Exercises .......................................................................1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值