Created
June 1, 2015 19:01
-
-
Save Solonarv/26efc11383d2e3796de3 to your computer and use it in GitHub Desktop.
literally ducks 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
class Quacks t where | |
quack :: t -> String | |
printQuack :: Quacks a => a -> IO () | |
printQuack thing = putStrLn (quack thing) | |
-- Define your Duck type somewhere | |
data Duck = {- whatever -} | |
instance Quacks Duck where | |
quack duck = "Quack! Quack, quaaack?" | |
-- Define some other type | |
data DuckBucket = DuckBucket { | |
bucketName :: String | |
bucketSize :: Int | |
bucketContents :: [Duck] | |
} | |
instance Quacks DuckBucket where | |
quack bucket = "All " ++ (show $ bucketSize bucket) ++ " ducks in " ++ bucketName bucket ++ " say 'Quack!'" | |
-- Now you can throw either a Duck or a DuckBucket at printQuack: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment