From 0903871a173e323b230b5ffce517ee1319cfd7f0 Mon Sep 17 00:00:00 2001 From: Ben Date: Wed, 9 Oct 2019 09:46:37 +0200 Subject: [PATCH] Create typeclasses.hs --- typeclasses.hs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 typeclasses.hs diff --git a/typeclasses.hs b/typeclasses.hs new file mode 100644 index 0000000..4040559 --- /dev/null +++ b/typeclasses.hs @@ -0,0 +1,29 @@ +class Playable a where + healingPower :: a -> Double + +data Warrior = + Warrior + { warriorName :: String + } + +instance Playable Warrior where + healingPower _warrior = 1 + +data Mage = + Mage + { mageName :: String + , wizardingPower :: Double + } + +instance Playable Mage where + healingPower mage = wizardingPower mage * 0.5 + +chris = Warrior { warriorName = "Chris" } +julie = Mage { mageName = "Julie", wizardingPower = 9000 } + +main = do + putStr "Julie's Healing Power: " + print $ healingPower julie + + putStr "Chris' Healing Power: " + print $ healingPower chris