[Haskell-beginners] Tuples .vs Either Vs datatypes
Christian.Maeder at dfki.de
Thu Apr 7 10:55:48 CEST 2011
Am 07.04.2011 00:33, schrieb Mike Meyer:
>> 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
I assumed that you replaced "Fail" (which corresponds to "Nothing" for
the Maybe data type) with "Error String" (without looking at your code),
so that you can attach a message to a failure.
data CharClass = Error String | Pattern String
which is isomorphic to ""Either String String", where "Left" capture
failure strings and "Right" captures valid patterns.
> is the same as "Maybe (Either Pattern Error)"? (Fail being a poorly
> chosen name - NoMatch would be better).
I don't think, that you need "Maybe (Either Pattern Error)", just code
the "Nothing" case as a suitable "error string".
Also "Either Pattern Error" has the wrong order. The "Error" should be
the "Left" component (by convention) for Monad instances to work as
expected! (This also applies to your "Either Bool Error" usage.)
The type synonyms Pattern and Error (being String) gain no additional
type safety. They only reveal (to the reader) that Either was used in
the wrong way, but would not stop you from using the Monad instances in
a wrong way.
>> 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.
As I said, "Left" is by convention the error case.
>> "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
The CharClass as above would be safer. (Nesting of Either in Maybe is
More information about the Beginners