[Haskell-beginners] Help in pattern matching

Stephen Tetley stephen.tetley at gmail.com
Mon Mar 8 09:16:45 EST 2010

On 8 March 2010 12:06, Magnus Therning <magnus at therning.org> wrote:


> What am I missing?

Hi Magnus

I was restating what Jonas mentioned but didn't elaborate - that Joe's
original program had a type error.

The solutions posted did two things:

1. Rewrite Joe's original (algebraic) data type to encode it with
products (aka pair - (,) ) and sums (Either) - this allowed some of
the pattern matching to be factored, reducing the number of cases in
the function test from 4 to 2

2. Correct the answer type to use a proper sum (Either Int String),
where previously some of declarations of test returned Int others
String (and so wouldn't type check).

Strictly speaking your program wasn't "equivalent" to the original
because it did step 2 and  would actually work!

Best wishes


More information about the Beginners mailing list