2014年2月20日木曜日

開発環境

Real World Haskell―実戦で学ぶ関数型言語プログラミング(Bryan O'Sullivan (著)、 John Goerzen (著)、 Don Stewart (著)、山下 伸夫 (翻訳)、伊東 勝利 (翻訳)、株式会社タイムインターメディア (翻訳)、オライリージャパン)の10章(コード事例研究: バイナリデータフォーマットの構文解析)、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 =
    case matchHeader "P2" s of
         Nothing -> Nothing
         Just s1 ->
             case getNat s1 of
                  Nothing -> Nothing
                  Just (width, s2) ->
                      case getNat (dropWhile isSpace s2) of
                           Nothing -> Nothing
                           Just (height, s3) ->
                               case getNat (dropWhile isSpace s3) of
                                    Nothing -> Nothing
                                    Just (maxGrey, s4)
                                        | maxGrey > 255 -> Nothing
                                        | otherwise ->
                                            case getInts (width * height)
                                                         (dropWhile isSpace s4) of
                                                 Nothing -> Nothing
                                                 Just ns -> 
                                                     Just (GreymapP2 width
                                                                     height
                                                                     maxGrey
                                                                     ns)

matchHeader :: String -> String -> Maybe String
matchHeader prefix str
    | prefix `isPrefixOf` str =
        Just (dropWhile isSpace (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 コメント:

コメントを投稿