Skip to content

Instantly share code, notes, and snippets.

@lgastako
Last active May 15, 2018 06:27
Show Gist options
  • Save lgastako/959be0cbf06ed12377c5f0872d6228a3 to your computer and use it in GitHub Desktop.
Save lgastako/959be0cbf06ed12377c5f0872d6228a3 to your computer and use it in GitHub Desktop.
type Enum a
= Enum
{ predFn : a -> Maybe a
, succFn : a -> Maybe a
}
pred : Enum a -> a -> Maybe a
pred (Enum { predFn }) =
predFn
succ : Enum a -> a -> Maybe a
succ (Enum { succFn }) =
succFn
intEnum : Enum Int
intEnum =
Enum
{ predFn = \x -> Just (x - 1)
, succFn = \x -> Just (x + 1)
}
boolEnum : Enum Bool
boolEnum =
Enum
{ predFn =
\b ->
if b then
Just False
else
Nothing
, succFn =
\b ->
if not b then
Just True
else
Nothing
}
@lgastako
Copy link
Author

> pred intEnum 10
Just 9 : Maybe.Maybe Int
> succ intEnum 10
Just 11 : Maybe.Maybe Int
> pred boolEnum True
Just False : Maybe.Maybe Bool
> pred boolEnum False
Nothing : Maybe.Maybe Bool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment