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

Anatoly Zaretsky anatoly.zaretsky at gmail.com
Tue May 19 09:03:52 UTC 2015


On Tue, May 19, 2015 at 2:20 AM, Cody Goodman
<codygman.consulting at gmail.com> wrote:
>
> data Codes = A0100A | A0500A deriving Show
>
> codeExists "A0100A" = True
> codeExists "A0500A" = True
> codeExists _ = False
>

data Codes = A0100A | A0500A deriving Read

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

Or with -XPatternGuards:

codeExists code | [(_, "")] <- reads code = True
                | otherwise = False


More information about the Haskell-Cafe mailing list