NEXT ·  UP ·  PREVIOUS ·  CONTENTS ·  INDEX

Defining a function by cases

Standard ML provides a mechanism whereby the notation which introduces the function parameter may constrain the type or value of the parameter by requiring the parameter to match a given pattern (so-called ``pattern matching''). The following function, day, maps integers to strings.

val day = fn 1 => "Monday"
           | 2 => "Tuesday"
           | 3 => "Wednesday"
           | 4 => "Thursday"
           | 5 => "Friday"
           | 6 => "Saturday"
           | _ => "Sunday";

The final case in the list is a catch-all case which maps any value other than those listed above it to "Sunday". Be careful to use double quotes around strings rather than single quotes. Single quote characters are used for other purposes in Standard ML and you may receive a strange error message if you use them incorrectly.

NEXT ·  UP ·  PREVIOUS ·  CONTENTS ·  INDEX