Skip to content

Instantly share code, notes, and snippets.

@fronx
Last active August 29, 2015 14:03
Show Gist options
  • Save fronx/bbbc782263afc75ba58d to your computer and use it in GitHub Desktop.
Save fronx/bbbc782263afc75ba58d to your computer and use it in GitHub Desktop.
{-# 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"
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