LightAndLight / ipso

A functional scripting language.
https://ipso.dev
16 stars 1 forks source link

Nested patterns in array patterns #354

Open LightAndLight opened 1 year ago

LightAndLight commented 1 year ago

When I implemented #81 I only implemented a single level of pattern matching:

case x of
  [a, b, c] -> ...
  ...

Supporting nested patterns seems pretty complicated:

case x of
  [1, 2, 3] -> ...
  [2, 2, 4] -> ...
  [] -> ...
  [_, _] -> ...
  _ -> ...

A single level of array patterns has a pretty good cost:benefit ratio right now. I'm noting that I might want nested pattern support in the future. The Implementation of Functional Programming Languages[^impl-fp] Chapter 5 has a good general algorithm for un-nesting pattern matches.

[^impl-fp]: Peyton Jones, S. L. (1987). The implementation of functional programming languages (prentice-hall international series in computer science). Prentice-Hall, Inc..

https://www.microsoft.com/en-us/research/uploads/prod/1987/01/slpj-book-1987.pdf
LightAndLight commented 8 months ago

Bump, I expected this today.

    bind args <- case args of
      [arg1, arg2] ->
        ...
      ["-h"] ->
        exitWithHelp (None ())
      ["--help"] ->
        exitWithHelp (None ())
      _ ->
        exitWithHelp (Some "expected 2 arguments, got ${int.toString (array.length args)}")