Standard ML allow you to pattern match on values, providing case anaylsis. Using the list
built into Standard ML, we could pattern match on its cases and apply a function to each value in the list.
nil
), there’s nothing to do: return the empty list.x
:
f
to x
map f
to the rest of the list xs
x'
, and the transformed rest of the list, xs'
x' :: xs'
It’s possible to pattern match directly in the function definition and avoid the intermediate values to make it less verbose, as in map'
.
(* examples/pattern-matching.sml *)