Instantly share code, notes, and snippets.

# kmizu/quiz.hs Created Jul 3, 2010

What would you like to do?
 -- Haskell Quiz http://blog.hackers-cafe.net/2010/06/haskell-quiz.html の答えっぽいもの -- Preludeの\$とコンフリクトするので隠す import Prelude hiding ((\$)) -- Showのような独自型クラスMyShowを定義 class MyShow a where myShow :: a -> String instance MyShow Bool where myShow arg = "True" instance MyShow Int where myShow arg = show arg infixr 0 \$ -- \$を再定義してないとは書いてない (\$) :: (MyShow a) => (String -> IO()) -> a -> IO() f \$ x = putStrLn (myShow x) a :: Int a = 0 b :: Int b = 1 c :: Int c = 0 d :: Int d = 0 main = do -- ここから print \$ a + c == 0 -- True. print \$ a == c -- True. a == c == 0. print \$ c == 0 -- True. Of course. print \$ a + d == 1 -- True. d == 1. print \$ b + c == 1 -- True. b == 1. print \$ b + d == 0 -- True. What happened?! print \$ b == d -- True. ?! print \$ d == 0 -- True. ?!?! -- ここまではある意味ダミー？ -- hints to make answer unique (perhaps...) -- 実は以下の部分だけで答えが[a, b, c, d] = [0, 1, 0, 0] に確定する print \$ sum([a, b, c]) -- 1 print \$ sum([a, b, d]) -- 1 print \$ [a, b, c] !! a -- 0 print \$ [a, b, c] !! b -- 1 print \$ [a, b, c] !! c -- 0