Skip to content

Instantly share code, notes, and snippets.

@Solonarv
Created June 1, 2015 19:01
Show Gist options
  • Save Solonarv/26efc11383d2e3796de3 to your computer and use it in GitHub Desktop.
Save Solonarv/26efc11383d2e3796de3 to your computer and use it in GitHub Desktop.
literally ducks in haskell
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