Created
February 18, 2012 01:57
-
-
Save avernet/1856860 to your computer and use it in GitHub Desktop.
Polymorphism in Haskell
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
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
module EasternOrder where | |
import Serializable | |
import Person | |
instance IsSerializable Person where | |
serialize (Person f l) = l ++ " " ++ f | |
easternOrder :: Person -> Serializable | |
easternOrder = Serializable |
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
module EuropeanOrder where | |
import Serializable | |
import Person | |
instance IsSerializable Person where | |
serialize (Person f l) = f ++ " " ++ l | |
europeanOrder :: Person -> Serializable | |
europeanOrder = Serializable |
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
import Person | |
import Serializable | |
import EuropeanOrder | |
import EasternOrder | |
us = europeanOrder (Person "Barack" "Obama") | |
cn = easternOrder (Person "Jintao" "Hu") | |
main = (print . serializeAll) [us, cn] |
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
import Person | |
import Serializable | |
import EuropeanOrder | |
main = (print . serialize) (Person "Barack" "Obama") |
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
module Person where | |
data Person = Person String String |
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
import Person | |
import Serializable | |
import EuropeanOrder | |
import EasternOrder | |
us = europeanOrder (Person "Barack" "Obama") | |
cn = easternOrder (Person "Jintao" "Hu") | |
main = (print . serializeAll) [us, cn] |
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
module Serializable where | |
import Data.List | |
class IsSerializable a where | |
serialize:: a -> String | |
data Serializable where | |
Serializable :: IsSerializable a => a -> Serializable | |
serializeAll:: [Serializable] -> String | |
serializeAll as = intercalate ", " (map (\(Serializable a) -> serialize a) as) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment