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 0 => "Monday"
           | 1 => "Tuesday"
           | 2 => "Wednesday"
           | 3 => "Thursday"
           | 4 => "Friday"
           | 5 => "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