Skip to content

Commit

Permalink
002A
Browse files Browse the repository at this point in the history
  • Loading branch information
mkut committed May 3, 2012
1 parent 6b27b94 commit 045e895
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
1 change: 1 addition & 0 deletions 002/A/.gitignore
@@ -0,0 +1 @@
main
34 changes: 34 additions & 0 deletions 002/A/Makefile
@@ -0,0 +1,34 @@
################################
## Makefile for Haskell
## Author: mkut
## Date : 2012.4.16
## Rev : 0.1
################################

## project settings
TARGET = main
SOURCES = haskell.hs
MILIBS = IO Contest

## command settings
HC = ghc
HCFLAGS = --make -O

## directory settings
MILIB_DIR = /home/mkut/milib/haskell

## file name macros
OBJS = $(SOURCES:%.hs=%.hi) $(SOURCES:%.hs=%.o)
MILIB_SRC = $(MILIBS:%=$(MILIB_DIR)/%.hs)

## make rules
.PHONY: default clean

default: $(TARGET)

$(TARGET): $(SOURCES) $(MILIB_SRC)
$(HC) $(HCFLAGS) -o $@ $^
rm $(OBJS)

clean:
rm -f *.o $(TARGET) $(OBJS) $(GEN_OBJS)
Empty file added 002/A/ans.txt
Empty file.
58 changes: 58 additions & 0 deletions 002/A/haskell.hs
@@ -0,0 +1,58 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE Rank2Types #-}
-- import Milib.Contest
-- import Milib.IO

import IO
import qualified Data.ByteString.Lazy.Char8 as C
import Text.Parsec.Prim
import Text.Parsec.Combinator
import Text.Parsec.Char
import Data.Functor.Identity
import Control.Monad
import Text.Printf

main = contestMain p solve number

solve y
| y `mod` 400 == 0 = True
| y `mod` 100 == 0 = False
| y `mod` 4 == 0 = True
| otherwise = False

p h True = hPutStrLn h "YES"
p h False = hPutStrLn h "NO"

-- Milib.Contest

type Printer a = Handle -> a -> IO ()
type Solver a b = a -> b
type Parser b = Stream C.ByteString Identity Char => Parsec C.ByteString () b
type CMain a b = Printer b -> Solver a b -> Parser a -> IO ()
type HCMain a b = Handle -> Handle -> CMain a b

instance Stream C.ByteString Identity Char where
uncons = return . C.uncons

hContestMain :: HCMain a b
hContestMain hin hout printer solver parser = do
input <- C.hGetContents hin
case parse parser "" input of
Left err -> do { hPutStr stderr "parse err: "; hPrint stderr err }
Right x -> printer hout $ solver x

contestMain :: CMain a b
contestMain = hContestMain stdin stdout

-- Milib.IO
number' :: Stream s m Char => ParsecT s u m Int
number' =
do ds <- many1 digit
return (read ds)
<?> "number"

number :: Stream s m Char => ParsecT s u m Int
number = do spaces; number'

-- vim: set expandtab:
Empty file added 002/A/in.txt
Empty file.

0 comments on commit 045e895

Please sign in to comment.