[Haskell-beginners] Tuples .vs Either Vs datatypes

Mike Meyer mwm at mired.org
Thu Apr 7 00:33:16 CEST 2011


On Wed, 06 Apr 2011 11:24:45 +0200
Christian Maeder <Christian.Maeder at dfki.de> wrote:

> Am 05.04.2011 19:41, schrieb Mike Meyer:
> [...]
> > data CharClass = Fail | Pattern String
> > matchCharClass :: Pattern ->  Char ->  CharClass
> 
> As mentioned by Alex Rozenshteyn this CharClass is isomorphic to "Maybe 
> String".

I think I missed the mail from Alex. But yes, that makes sense. And
learning how to use the built-in types is part of learning the
language.

> > This only required minor changes to the code, but made it easy to add
> > "Error String" to the CharClass datatype later. That version can be
> 
> Replacing Fail (or Nothing) by "Error String" is like going to "Either 
> String String".

Ok this confuses me. What does "or Nothing" by "Error String" mean?

data CharClass = Fail | Pattern String | Error String

is the same as "Maybe (Either Pattern Error)"? (Fail being a poorly
chosen name - NoMatch would be better).

> Yet, user-defined data types (no type synonyms!) may increase 
> readability (and type safety). However, one disadvantage is that some 
> type class instances have to be redefined (or derived) if needed.

Right. I prefer adding the Pattern & Error types, so they document
which of Left and Right should be used here.

> "Maybe" and "Either String" are fairly standard (and have Monad and what 
> not instances), still your data type CharClass is perfect (if it serves 
> the purpose).
> HTH Christian

Yup.  Very educational. Thanks.

> > seen at http://pastebin.com/eyre8795 (as always, critiques welcome).

This has been updated to use the 'Maybe (Either Pattern Error)" return
values.

	Thank you.
	<mike
-- 
Mike Meyer <mwm at mired.org>		http://www.mired.org/consulting.html
Independent Software developer/SCM consultant, email for more information.

O< ascii ribbon campaign - stop html mail - www.asciiribbon.org



More information about the Beginners mailing list