++ in patterns
In Haskell we are used to handling lists with
: as our pattern. Curry1 can do this but it can also use
++ in patterns. This can be used to pattern match longer prefixes for strings, e.g. this Haskell pattern:
f('a':'b':'c':x) = ...
works in Curry, but can be shorter as:
However it can also match much more exotic things.
This pattern matches all strings that have
abc as a substring somewhere. This can't even be written as a pattern in Haskell and requires a guard at the minimum.
This pattern matches lists ending with the element
b. It's the sort of pattern that Haskell programmers can attest would certainly be useful for many things.
This can be combined with other tricks to get even more powerful patterns, for example:
This matches strings which are made up two copies of the same string concatenated.
++ patterns aren't just a way to write certain patterns more efficiently. They allow us to represent much more complex patterns on lists than were even possible before.
++ can be used as a pattern in PAKCS, KiCS, and MCC Curry, but does not work in Sloth, so this tip is not applicable there.