你能在一个接受数据类型卡并返回布尔值的函数中公式化一个case表达式吗


datatype cards = king of int * int
               | queen of string
               | jack of cards
               | ace of cards * cards
               | joker of int * cards

当然。

fun hasKing cards =
    case cards of
         king (i, j)          => true
       | queen s              => false
       | jack cards1          => hasKing cards1
       | ace (cards1, cards2) => hasKing cards1 orelse hasKing cards2
       | joker (i, cards2)    => hasKing cards2

然而,我会用大写字母命名我的值构造函数,以将它们与函数区分开来:

datatype cards = King of int * int
               | Queen of string
               | Jack of cards
               | Ace of cards * cards
               | Joker of int * cards

最新更新