Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Learning Mode] Discussion: How do we best introduce case-expressions? #1102

Open
kytrinyx opened this issue Aug 30, 2022 · 5 comments
Open
Labels

Comments

@kytrinyx
Copy link
Member

The goal of this issue is to come up with a code example that can be used as the basis for an exercise that introduces case-expressions in Haskell.

A few thoughts:

  • We don't have to teach everything about case expressions (and indeed, we probably shouldn't). We'd rather have several smaller exercises that build on each other than one big exercise. This gets people writing code quickly, which helps the concept to start sinking in
  • We'd like to introduce this as early as possible in the concept tree, maybe right after Temperature, which introduces a couple of basic numeric types (Int and Float). But if the example code is easier to do using something other than numbers, then we can introduce another exercise first, before case-expressions.
  • The code doesn't need to be ideal, idiomatic Haskell. This is a tiny stepping stone towards understanding case in Haskell, but students will quickly move beyond this into code that uses more of the language, and uses the language in ways that are more typical.

Elixir introduces case expressions with this exercise:
https://exercism.org/tracks/elixir/exercises/german-sysadmin
The example solution looks like this:
https://github.com/exercism/elixir/blob/main/exercises/concept/german-sysadmin/.meta/exemplar.ex

Is this a bit of code that would translate well into Haskell? (Would it feel like decent Haskell, or would it feel weird and foreign?)

What are some other sample bits of code that use case in a way that is potentially easy to understand for people who are new to Haskell?

@MatthijsBlom
Copy link
Contributor

We don't have to teach everything about case expressions (and indeed, we probably shouldn't).

It might surprise you how easy it is to explain (essentially) everything about case expressions in Haskell. They're quite simple.


For reference, this is the most recent concensus on Slack (original):

Alright, so pattern matching is to be split up into two concepts, with

  • Pattern Matching 1 introducing matching on integer literals and (hereby proposed by me) guards, and
  • Pattern Matching 2 introducing matching on ADTs (including reminder on guards)

(Names temporary: I failed to come up with good name pairs.)

Does this sound right?

If we do this we should consider introducing custom data types/algebraic data types between the two, and Maybe in particular as it is not an enum-like type but has an optional field.

Also Either, in parallel with or directly following Maybe. Having them both as prerequisites for PM2 is not necessary but would be nice.

@pwadsworth has already worked on this PM1: the corresponding exercise is Guessing Game.

I'd like to call dibs on the explainers of PM2, as I have already been composing in my head for days. These explainers should make it obvious even to the non-Haskellers what appropriate exercises look like.

@pwadsworth
Copy link
Contributor

pwadsworth commented Aug 31, 2022

For names, how about:

  • Pattern Matching: Literals and Variables.
  • Pattern Matching: Types.

The Type part would benefit of a qualifier. Simple? Basic? So is not confusing if we later want to go into PM: Lists, PM: Tuples, etc.
Although after these two concepts it would make sense to include the specifics of pattern matching with each new type introduced.

@MatthijsBlom

I'd like to call dibs on the explainers of PM2

What do you mean by 'explainers'? The concept? instructions? both? Do you want me to tackle the exercise? If so, Valentine's Day?

@ErikSchierboom
Copy link
Member

ErikSchierboom commented Aug 31, 2022

I'm not entirely sure I follow the discussion.

Alright, so pattern matching is to be split up into two concepts, with

  • Pattern Matching 1 introducing matching on integer literals and (hereby proposed by me) guards, and
  • Pattern Matching 2 introducing matching on ADTs (including reminder on guards)

The guessing-game exercise has been built and covers part 1, and the valentines-day exercise that is still to be built will cover part 2. That said, this issue is about case-expressions, for which I thought we intended to create another, third exercise just for case-expressions. Did I miss anything?

@MatthijsBlom
Copy link
Contributor

Way I understand it, the concept graph will look something like this.

              |
   Simple Pattern Matching      
   /          |        \
Maybe      Either      ... (tuples, lists?)
   \          |        /
    Algebraic Data Types
      ("custom types")
              |
   Pattern Matching Proper
              |

with

  • Simple Pattern Matching introducing matching on literals and guards. Personally I prefer introducing case expressions here as well, and explicitly presenting the name pattern = ... as syntactic sugar for case.

  • Maybe, Either, …, introducing the following aspects of these types:

    But not pattern matching on them.

  • Algebraic Data Types introducing data declarations, maybe type declarations, and probably not newtype declarations. But still not pattern matching on them.

  • Pattern Matching Proper introducing pattern matching on algebraic data types. Exhaustivity, recursive ('deep') matching, pattern variables, …, all the usual stuff.


What do you mean by 'explainers'? The concept? instructions? both? Do you want me to tackle the exercise? If so, Valentine's Day?

@pwadsworth The explainers are the about.md and the introduction.md. I have no wish one way or the other as to who does the exercise.

Did I miss anything?

@ErikSchierboom in the above-laid-out design, case expressions are tackled at the same time as pattern matching. I feel they should: 'all' pattern matching occurs in case expressions or syntactic sugar for the latter. Valentine's Day is fine.

@kytrinyx
Copy link
Member Author

@MatthijsBlom I really like that concept graph, and I'd like to open a new issue with the graph as a starting point.

We have a solid exercise for the first node in the graph with the Guessing Game PR that @pwadsworth has opened (#1094), and I'd like to get that merged. We can always come back and tweak it once we have more exercises and start seeing how the bigger picture is shaping up.

I'll leave this issue on hold until we've figured out the exercises for the nodes in the graph. As you say, case expressions will likely be covered as part of that, and if so we can just close this issue at that point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants