Pattern matching identical values

This is called a nonlinear pattern. There have been several threads on the haskell-cafe mailing list about this, not long ago. Here are two: http://www.mail-archive.com/haskell-cafe@haskell.org/msg59617.html http://www.mail-archive.com/haskell-cafe@haskell.org/msg62491.html Bottom line: it’s not impossible to implement, but was decided against for sake of simplicity. By the way, you do not need if or case to work around this; … Read more

Haskell GHC: what is the time complexity of a pattern match with N constructors?

A jump table is used, making the pattern-match a constant time operation. Unfortunately I’m unable to find an up-to-date citation for this, although this page mentions the implementation of Cmm-level switch statements as jump tables, and this old tagging design document uses a case on a Bool as an example, producing a jump table.

What are Alternative’s “some” and “many” useful for?

TL;DR: some is one or more, many is 0 or more results collected from performing the same computation over and over by the familiar maximal munch rule. For this to make sense, some state passing (and alteration) must take place reducing the domain of possibilities somehow, otherwise it will repeat ad infinitum. And state passing … Read more

Can you overload + in haskell?

Yes (+) is part of the Num typeclass, and everyone seems to feel you can’t define (*) etc for your type, but I strongly disagree. newtype Pair a b = Pair (a,b) deriving (Eq,Show) I think Pair a b would be nicer, or we could even just use the type (a,b) directly, but… This is … Read more

Integral operators quot vs. div

To quote section 6.4.2 from the Haskell report: The quot, rem, div, and mod class methods satisfy these laws if y is non-zero: (x `quot` y)*y + (x `rem` y) == x (x `div` y)*y + (x `mod` y) == x quot is integer division truncated toward zero, while the result of div is truncated … Read more