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

Replace individual list elements in Haskell?

Typically, you modify elements of a list by splitting the list, replacing an element, and joining it back together. To split a list at an index, we have: splitAt :: Int -> [a] -> ([a], [a]) which you can use to break up a list, like so: > splitAt 2 [“Off”,”Off”,”Off”,”Off”] ([“Off”,”Off”],[“Off”,”Off”]) now you just … Read more

Python faster than compiled Haskell?

The Original Haskell Code There are two issues with the Haskell version: You’re using string IO, which builds linked lists of characters You’re using a non-quicksort that looks like quicksort. This program takes 18.7 seconds to run on my Intel Core2 2.5 GHz laptop. (GHC 7.4 using -O2) Daniel’s ByteString Version This is much improved, … Read more

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