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

时间:2016-03-31 05:20:50

标签: sml smlnj

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

1 个答案:

答案 0 :(得分:2)

不确定

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