Created
May 25, 2017 16:50
-
-
Save Biacco42/3e5133be27d1b775fef54297764be3ee to your computer and use it in GitHub Desktop.
Haskell Type Class Study
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
{-# 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