Skip to content

Instantly share code, notes, and snippets.

@Biacco42
Created May 25, 2017 16:50
Show Gist options
  • Save Biacco42/3e5133be27d1b775fef54297764be3ee to your computer and use it in GitHub Desktop.
Save Biacco42/3e5133be27d1b775fef54297764be3ee to your computer and use it in GitHub Desktop.
Haskell Type Class Study
{-# LANGUAGE FlexibleInstances #-}
main = do
print $ mySum ([1, 2, 3] :: [Int])
print $ mySum (["hoge", "piyo", "huga"] :: [String])
class AddMonoid a where
unit :: a
add :: a -> a -> a
instance AddMonoid Int where
unit = 0
add x y = x + y
instance AddMonoid [Char] where
unit = ""
add x y = x ++ y
mySum :: AddMonoid a => [a] -> a
mySum [] = unit
mySum (x: xs) = add x (mySum xs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment