Skip to content

Instantly share code, notes, and snippets.

@essic
Last active April 9, 2017 22:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save essic/bb4be773869c1f8170a9e88b922f7a98 to your computer and use it in GitHub Desktop.
Save essic/bb4be773869c1f8170a9e88b922f7a98 to your computer and use it in GitHub Desktop.
Another dumb example of ADT created by essic - https://repl.it/HCZ4/16
data Race = White | Black | Asian | Indian | Unknown
deriving (Show)
data HumanInformations = HumanInformations
{
name :: String , age :: Int , weight :: Int, race :: Race
} deriving(Show)
data Human = Male HumanInformations | Female HumanInformations
deriving(Show)
data LivingBeings = Monkey String | Tiger String | Human Human | Bear
deriving (Show)
getName :: LivingBeings -> String
getName x =
case x of
Monkey a -> a
Tiger a -> a
Human h ->
case h of
Female f -> name f
_ -> "Unknown human creature"
_ -> "Unknown creature"
aly :: LivingBeings
aly = Human $ Male $ HumanInformations "Aly-Bocar Cisse" 31 110 Black
adeline :: LivingBeings
adeline = Human ( Female (HumanInformations "Adeline Daime" 31 55 White) )
aTigerNamedCharly :: LivingBeings
aTigerNamedCharly = Tiger "Charly"
aMonkeyNameChita :: LivingBeings
aMonkeyNameChita = Monkey "Chita"
main :: IO ()
main = do
print $ getName aly
print $ getName adeline
print $ getName aMonkeyNameChita
print $ getName aTigerNamedCharly
print $ getName Bear
print "Hello world"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment