Created
January 1, 2013 18:55
-
-
Save tos-kamiya/4429328 to your computer and use it in GitHub Desktop.
Komachi-zan 2013.
Run as follows: $ runghc komachi2013gen.hs | runghc komachi2013chk.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# Language TemplateHaskell, QuasiQuotes, FlexibleContexts #-}↲ | |
↲ | |
import Control.Monad↲ | |
import Text.Peggy↲ | |
↲ | |
[peggy|↲ | |
expr :: Double↲ | |
= expr "+" fact { $1 + $2 }↲ | |
/ expr "-" fact { $1 - $2 }↲ | |
/ fact↲ | |
↲ | |
fact :: Double↲ | |
= fact "*" number { $1 * $2 }↲ | |
/ number↲ | |
↲ | |
number ::: Double↲ | |
= [1-9] [0-9]* { read ($1 : $2) }↲ | |
|]↲ | |
↲ | |
main = do↲ | |
inputLines <- getContents↲ | |
-- print . lines $ inputLines↲ | |
forM_ (lines inputLines) $ \line -> do↲ | |
case parseString expr "<stdin>" line of↲ | |
Right 2013 -> putStrLn line↲ | |
_ -> return ()↲ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.Char↲ | |
↲ | |
genexpr n s =↲ | |
if (n == 10)↲ | |
then do↲ | |
putStrLn $ reverse s↲ | |
else do↲ | |
let c = chr $ n + ord '0'↲ | |
let s1 = c:s↲ | |
genexpr (n + 1) s1↲ | |
let s2 = c:'-':s↲ | |
genexpr (n + 1) s2↲ | |
let s3 = c:'+':s↲ | |
genexpr (n + 1) s3↲ | |
let s4 = c:'*':s↲ | |
genexpr (n + 1) s4↲ | |
↲ | |
main = do↲ | |
genexpr 2 "1"↲ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment