Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Standard ML
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Pattern matching=== Patterns are matched in the order in which they are defined. [[C (programming language)|C]] programmers can use [[tagged union]]s, dispatching on tag values, to do what ML does with datatypes and pattern matching. Nevertheless, while a C program decorated with appropriate checks will, in a sense, be as robust as the corresponding ML program, those checks will of necessity be dynamic; ML's [[Static program analysis|static checks]] provide strong guarantees about the correctness of the program at compile time. Function arguments can be defined as patterns as follows: <syntaxhighlight lang="sml"> fun area (Circle (_, r)) = Math.pi * square r | area (Square (_, s)) = square s | area (Triangle p) = heron p (* see above *) </syntaxhighlight> The so-called "clausal form" of function definition, where arguments are defined as patterns, is merely [[syntactic sugar]] for a case expression: <syntaxhighlight lang="sml"> fun area shape = case shape of Circle (_, r) => Math.pi * square r | Square (_, s) => square s | Triangle p => heron p </syntaxhighlight> ====Exhaustiveness checking==== Pattern-exhaustiveness checking will make sure that each constructor of the datatype is matched by at least one pattern. The following pattern is not exhaustive: <syntaxhighlight lang="sml"> fun center (Circle (c, _)) = c | center (Square ((x, y), s)) = (x + s / 2.0, y + s / 2.0) </syntaxhighlight> There is no pattern for the {{code|Triangle}} case in the {{code|center}} function. The compiler will issue a warning that the case expression is not exhaustive, and if a {{code|Triangle}} is passed to this function at runtime, {{code|lang=sml|exception Match}} will be raised. ====Redundancy checking==== The pattern in the second clause of the following (meaningless) function is redundant: <syntaxhighlight lang="sml"> fun f (Circle ((x, y), r)) = x + y | f (Circle _) = 1.0 | f _ = 0.0 </syntaxhighlight> Any value that would match the pattern in the second clause would also match the pattern in the first clause, so the second clause is unreachable. Therefore, this definition as a whole exhibits redundancy, and causes a compile-time warning. The following function definition is exhaustive and not redundant: <syntaxhighlight lang="sml"> val hasCorners = fn (Circle _) => false | _ => true </syntaxhighlight> If control gets past the first pattern ({{code|Circle}}), we know the shape must be either a {{code|Square}} or a {{code|Triangle}}. In either of those cases, we know the shape has corners, so we can return {{code|lang=sml|true}} without discerning the actual shape.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)