Skip to content

Instantly share code, notes, and snippets.

@kmizu
Created July 3, 2010 20:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmizu/462815 to your computer and use it in GitHub Desktop.
Save kmizu/462815 to your computer and use it in GitHub Desktop.
-- 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment