Last active
April 9, 2017 22:02
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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