CL Newsletter, September 2022 - Pattern Matching Part 2

Last year, we shared with you the CL pattern library and a little bit on how you can use it. This month, we want to follow up with some tips (and a few examples) to help you as you continue your expression parsing journey.

Tip #1: Use Other Tools

Stuck trying to list out every possible detail in a pattern? Try parsing the expression down to its general form and then using other CL tools to interpret the details. For example:

If you’re trying to interpret the linear expression term of the product of an integer and a linear expression for x:

Why worry about all of these: When you can use xyLine:
3x+1 line = xyLine(linear term)
x+2x+1 m = Line.slope
x+1+2x c = line.yIntercept

Try It Out!

Tip #2: Beware of Over Specifying

The more specific your pattern is, the greater the risk of excluding an answer in a form that should be included. Even relatively simple inputs can be confused. Take patterns.number, for example:

Matches Does Not Match
:white_check_mark: 2.13 :x: ½
:white_check_mark: 10000 :x: 2 ¾
:white_check_mark: -9.0 :x:

And use the .literal pattern very carefully! If you’re not careful, you could be creating a thinly veiled direct string comparison!

There are two things you can do to avoid this problem:

  1. Use (at least) two patterns per task: a specific but permissive pattern to match and a general, more flexible pattern to parse (parsing only inputs that also match your first pattern).
  2. Use patterns to write error messages and warnings rather than to mark correctness. That way, students entering responses in forms you don’t want will at least know they need to change something.

New Toys!

We wanted to leave you with more than plain advice, so we’ve compiled a few of our favorite patterns that you can use to help you parse an input. Enjoy!

Get The Collection

If you have any questions or want to learn more about the pattern library, please don’t hesitate to email Jay, John, Leah, or Schuyler.