Skip to content

Instantly share code, notes, and snippets.

@nishio
Created July 13, 2010 00:29
Show Gist options
  • Save nishio/473281 to your computer and use it in GitHub Desktop.
Save nishio/473281 to your computer and use it in GitHub Desktop.
-- Quiz: http://blog.hackers-cafe.net/2010/06/haskell-quiz.html
-- Answer: http://blog.hackers-cafe.net/2010/07/haskell-quiz-answer.html
class Foo t where
a :: t
b :: t
instance Foo Int where
a = 0
b = 1
instance Foo Integer where
a = 1
b = 0
c :: Int
c = 0
d :: Integer
d = 0
main = do
print $ a + c == 0 -- True.
print $ a == c -- True. a::Int == c::Int == 0.
print $ c == 0 -- True.
print $ a + d == 1 -- True.
-- d is Integer. So the variable a is a::Integer,
-- which equals 1. "d == 1" is a misconception.
print $ b + c == 1 -- True. b::Int is 1.
print $ b + d == 0 -- True. However, b::Integer is 0.
print $ b == d -- True. Yes, both are 0::Integer;
print $ d == 0 -- True.
-- Why I use "d == 0" not "b == 0"?
-- Because it causes "ambiguous type" error.
-- You know, ":t 0" is "(Num t) => t". No way to determine
-- it is Int or Integer.
-- hints to make answer unique (perhaps...)
print $ sum([a, b, c]) -- 1: It is [0, 1, 0]::[Int]
print $ sum([a, b, d]) -- 1: It is [1, 0, 0]::[Integer]
print $ [a, b, c] !! a -- 0: (!!) take Int argument,
print $ [a, b, c] !! b -- 1: so it says (a::Int /= b::Int)
print $ [a, b, c] !! c -- 0
-- Why I didn't show [a, b, c] !! d is,
-- it causes error because d is Integer.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment