0
votes

I'm playing around with datatypes in SML for the first time. I've defined a datatype called suit with the four suits of playing cards

datatype suit = Spades | Clubs | Hearts | Diamonds;

Now I want to write a function that takes a suit and returns a string representation of it. Here it is currently

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";

It compiles fine but when I try to call it with each suit

suitname Hearts;
suitname Spades;
suitname Clubs;
suitname Diamonds;

I get this error

enter image description here

and have no idea why. Any help would be appreciated greatly!

1
You can shorten the if-else chain to case x of Spades => "Spades" | Clubs => "Clubs" | Diamonds => "Diamonds" | Hearts => "Hearts"Colonel Thirty Two

1 Answers

0
votes

This error message looks like you defined the datatype suit twice (perhaps interactively?), and the definition of the function is using the older definition, and the call sites the newer. Try to reenter the function definition.