[Haskell-cafe] Automatically generate sum type members -> [String]

Anatoly Zaretsky anatoly.zaretsky at gmail.com
Tue May 19 09:12:21 UTC 2015


On Tue, May 19, 2015 at 12:03 PM, Anatoly Zaretsky
<anatoly.zaretsky at gmail.com> wrote:
>
> data Codes = A0100A | A0500A deriving Read
>
> codeExists code =
>   case reads code of
>     [(_, "")] -> True
>     _ -> False
>
> Or with -XPatternGuards:
>
> codeExists code | [(_, "")] <- reads code = True
>                 | otherwise = False

Oops, should have checked the types before posting:

codeExists code =
  case reads code :: [(Codes, String)] of
    [(_, "")] -> True
    _ -> False

Otherwise the compiler could not guess the right Read instance.


More information about the Haskell-Cafe mailing list