haskellfoundation / HaskellSchool

Apache License 2.0
50 stars 11 forks source link

Pattern matching: What, where, how #13

Open Kleidukos opened 3 years ago

Kleidukos commented 3 years ago

Basically, there are two types of pattern matching techniques that we want to cover:

  1. Destructuring ADTs and lists (and especially user-defined datatypes
  2. Other techniques for pattern matching, such as:
    • Functions Guards
    • Pattern Guards
    • As-patterns
    • View Patterns

The first type should be mentioned in the lesson about user-defined ADTs.
The second type will need its own lesson.


cc @DrewFenwick

DrewFenwick commented 3 years ago

Good learning materials on all the forms of pattern matching are spread out all over the place and somewhat hard to discover if you aren't specifically looking for them: Currently view patterns are best documented by the GHC user's guide, as-patterns are coverd by A Gentle Introduction to Pattern Matching and the Haskell Wikibook while Pattern Guards are mentioned a little hither and thither but generally hard to find or even hear of at all unless you're specifically looking for them (which is surprising for an unextended Report Haskell feature!)

Having all forms of pattern matching covered by Haskell School would fill a role that isn't currently filled very well in my opinion. If lesson headings were such that someone searching specifically for "patterns" could find all our coverage of them too, that would be even better.

Lesson Order

In tutorials, list matching patterns generally get introduced fairly early; often earlier than ADTs. Covering how to pattern match on all forms of ADT and explaining that patterns can only take apart what constructors put together requires an understanding of constructors that is probably best gained from learning how to make ADTs.

We'd be tying our hands too much to only show pattern matching until after ADTs, but violating the pedagogic order to introduce pattern matching on ADTs before introducing ADTs, so things need to be split.

On talking with @Kleidukos, IIRC we felt that the appropriate order was to introduce pattern matching on lists early as nessecary, cover pattern matching on ADTs in the same section that introduces ADTs, and have an "advanced pattern matching" section that covered all the myriad special pattern matching syntaxes.

Maybe we ought to have a styled "interesting note" box in the ADT section that points out that the : operator and [] value are just constructors of the list type, which is a built in sum type, and that's why pattern matching on (x:xs) works, while xs ++ ys cannot?

More topics

Here are some more pattern matching topics we might want to cover:

All of the topics we cover will need an appropriate home in the lesson order, and any topics covering language extension features need to appropriately inform the user.

Kleidukos commented 2 years ago

Fixed by #32