【发布时间】:2015-10-24 16:54:35
【问题描述】:
我是第一次在 SML 中使用数据类型。我用四张扑克牌定义了一个名为 suit 的数据类型
datatype suit = Spades | Clubs | Hearts | Diamonds;
现在我想编写一个函数,它接受西装并返回它的字符串表示形式。目前是这里
fun suitname(x) =
if x = Spades then
"Spades"
else if x = Clubs then
"Clubs"
else if x = Diamonds then
"Diamonds"
else if x = Hearts then
"Hearts"
else
"Undefined suit";
它编译得很好,但是当我尝试用每套西装调用它时
suitname Hearts;
suitname Spades;
suitname Clubs;
suitname Diamonds;
我收到此错误
不知道为什么。任何帮助将不胜感激!
【问题讨论】:
-
可以将if-else链缩短为
case x of Spades => "Spades" | Clubs => "Clubs" | Diamonds => "Diamonds" | Hearts => "Hearts"