Last active
August 29, 2015 14:03
-
-
Save fronx/bbbc782263afc75ba58d to your computer and use it in GitHub Desktop.
This file contains hidden or 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 MultiParamTypeClasses #-} | |
{-# LANGUAGE FunctionalDependencies #-} | |
{-# LANGUAGE FlexibleContexts #-} | |
{-# LANGUAGE FlexibleInstances #-} | |
import Prelude hiding ((*)) | |
import qualified Prelude as P | |
class Mul a b c | a b -> c where | |
(*) :: a -> b -> c | |
instance Mul Int Int Int where | |
x * y = x P.* y | |
instance Mul String Int String where | |
x * 1 = x | |
x * n | n <= 0 = "" | |
| n > 0 = x ++ x * (n - 1) | |
-- The type can be inferred. | |
-- foo :: Mul a Int c => a -> c | |
foo x = x * (2::Int) | |
main = do | |
print $ foo (3::Int) | |
print $ foo "hi" |
This file contains hidden or 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
def foo(x): | |
return x * 2 | |
print foo(3) | |
print foo("hi") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment