Example of Icon Style Pattern Matching

In the pattern expression languages, pattern operations are part of the language in the same way as arithmetic operations, like +:

postalcode := "H3G 1V7";
if postalcode ?
   (part1 := (tab(any(&letters)) || tab(any(&digits)) ||
              tab(any(&letters))) & (=" " | "") &
    part2 := (tab(any(&digits)) || tab(any(&letters)) ||
              tab(any(&digits))) & pos(0))
then {postalcode := part1 || part2; write(postalcode)}
else {write("Invalid postalcode: " || postalcode)}
All About Pattern Matching Sam Wilmott
Slide 19 of 37        www.wilmott.ca