2014年2月21日金曜日

開発環境

Real World Haskell―実戦で学ぶ関数型言語プログラミング(Bryan O'Sullivan (著)、 John Goerzen (著)、 Don Stewart (著)、山下 伸夫 (翻訳)、伊東 勝利 (翻訳)、株式会社タイムインターメディア (翻訳)、オライリージャパン)の10章(コード事例研究: バイナリデータフォーマットの構文解析)、10.3(「ボイラープレート」コードの除去)、10.9(今後の方向性)の練習問題 1.を解いてみる。

その他参考書籍

練習問題 1.

前回のコードから、「ボイラープレート」コードを除去。

コード(BBEdit, Emacs)

PGMP2.hs

{-# OPTIONS -Wall -Werror #-}
module PGMP2 where

import Data.Char (isSpace, isDigit)
import Data.List (isPrefixOf)
import System.Environment (getArgs)

main :: IO ()
main = do
    (a:_) <- getArgs
    contents <- readFile a
    let g = parseP2 contents
    print g
    putStrLn "データ"
    print $ fmap greyData g
    
-- プレーンフォーマット
data GreymapP2 = GreymapP2 {greyWith :: Int,
                            greyHeight :: Int,
                            greyMax :: Int,
                            greyData :: [Int]}
                 deriving (Eq)

instance Show GreymapP2 where
    show (GreymapP2 w h m _) = "GreymapP2 " ++ show w ++ "x" ++ show h ++
                               " " ++ show m

parseP2 :: String -> Maybe GreymapP2
parseP2 s =
    matchHeader "P2" s              >>?
    \s1 -> skipSpace ((), s1)       >>?
    (getNat . snd)                  >>?
    skipSpace                       >>?
    \(width, s2) -> getNat s2       >>?
    skipSpace                       >>?
    \(height, s3) -> getNat s3      >>?
    over                            >>?
    skipSpace                       >>?
    \(maxGrey, s4) ->
        getInts (width * height) s4 >>?
    \ns -> Just (GreymapP2 width height maxGrey ns)

(>>?) :: Maybe a -> (a -> Maybe b) -> Maybe b
Nothing >>? _ = Nothing
Just x >>? f = f x

skipSpace :: (a, String) -> Maybe (a, String)
skipSpace (x, s) = Just (x, dropWhile isSpace s)

over :: (Int, b) -> Maybe (Int, b)
over (n, s) | n > 255 = Nothing
            | otherwise = Just (n, s)

matchHeader :: String -> String -> Maybe String
matchHeader prefix str
    | prefix `isPrefixOf` str =
        Just (drop (length prefix) str)
    | otherwise = Nothing

-- nat(natural number)
getNat :: String -> Maybe (Int, String)
getNat s = let (num, str) = span isDigit s
           in if null num 
              then Nothing
              else Just (read num :: Int, str)

getInts :: Int -> String -> Maybe [Int]
getInts n s = iter n (' ':s) []

iter :: Int -> String -> [Int] -> Maybe [Int]
iter n s ns
    | n == 0 && (null s || null (dropWhile isSpace s)) = Just ns
    | n /= 0 && null s = Nothing
    | n /= 0 && (isSpace (head s)) =
        let (num, str) = span isDigit (tail s)
        in if null num
        then Nothing
        else iter (n - 1) str (ns ++ [read num :: Int])
    | otherwise = Nothing

入出力結果(Terminal, runghc)

$ runghc PGMP2.hs test.pgm 
Just GreymapP2 24x7 15
データ
Just [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,3,3,0,0,7,7,7,7,0,0,11,11,11,11,0,0,15,15,15,15,0,0,3,0,0,0,0,0,7,0,0,0,0,0,11,0,0,0,0,0,15,0,0,15,0,0,3,3,3,0,0,0,7,7,7,0,0,0,11,11,11,0,0,0,15,15,15,15,0,0,3,0,0,0,0,0,7,0,0,0,0,0,11,0,0,0,0,0,15,0,0,0,0,0,3,0,0,0,0,0,7,7,7,7,0,0,11,11,11,11,0,0,15,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
$ cat test.pgm
P2
24 7
15
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 3 3 3 3 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 15 15 15 0
0 3 0 0 0 0 0 7 0 0 0 0 0 11 0 0 0 0 0 15 0 0 15 0
0 3 3 3 0 0 0 7 7 7 0 0 0 11 11 11 0 0 0 15 15 15 15 0
0 3 0 0 0 0 0 7 0 0 0 0 0 11 0 0 0 0 0 15 0 0 0 0
0 3 0 0 0 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
$

0 コメント:

コメントを投稿