11
\$\begingroup\$

Curry is the Language of the Month this month and some users are starting to get into it, so let's get some together.

Haskell and Curry are similar in a lot of ways and a lot of tips for Haskell work well in Curry and vice versa. So I'd like in this question to gather tips for in Curry, that would be useful to someone who has read up on our Haskell tips extensively.

This would cover cool things you can do in Curry that you can't do in Haskell, as well as tips for getting around the cool things you can't do in Curry that you might be used to from Haskell.

Please post only one tip per answer.

\$\endgroup\$
1
  • 5
    \$\begingroup\$ does Curry support function haskelling? \$\endgroup\$
    – user253751
    Apr 6, 2022 at 9:07

2 Answers 2

11
\$\begingroup\$

Use ++ 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:

f("abc"++x)=...

However it can also match much more exotic things.

f(x++"abc"++y)=...

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.

f(a++[b])=...

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:

f(a++a)=...

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.


1: ++ 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.

\$\endgroup\$
4
\$\begingroup\$

Use ? as or-patterns

In languages like Mathematica or Rust or Python 3.10, there are some kind of "or-patterns", usually written in the form a|b, that could match either a or b. Haskell doesn't have such thing. But in Curry, you can write a?b.

For example, instead of

f a=someExpr a
f b=someExpr b

You can write:

f c@(a?b)=someExpr c

Unlike other languages, ? isn't a special symbol for patterns. It is just a built-in function that returns either of the two values. Using a?b in patterns is just another example of functional patterns, just like a++b in Wheat Wizard's answer.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.