Real World Haskell
実戦で学ぶ関数型言語プログラミング
(オライリージャパン)
Bryan O'Sullivan (著) John Goerzen (著)
Don Stewart (著)
山下 伸夫 (翻訳) 伊東 勝利 (翻訳)
株式会社タイムインターメディア (翻訳)
開発環境
- OS X Mavericks - Apple(OS)
- BBEdit - Bare Bones Software, Inc., Emacs (Text Editor)
- Haskell (純粋関数型プログラミング言語)
- GHC (The Glasgow Haskell Compiler) (処理系)
- The Haskell Platform (インストール方法、モジュール等)
Real World Haskell―実戦で学ぶ関数型言語プログラミング(Bryan O'Sullivan (著)、 John Goerzen (著)、 Don Stewart (著)、山下 伸夫 (翻訳)、伊東 勝利 (翻訳)、株式会社タイムインターメディア (翻訳)、オライリージャパン)の10章(コード事例研究: バイナリデータフォーマットの構文解析)、10.3(「ボイラープレート」コードの除去)、10.9(今後の方向性)の練習問題 1.を解いてみる。
その他参考書籍
- すごいHaskellたのしく学ぼう!(オーム社) Miran Lipovača(著)、田中 英行、村主 崇行(翻訳)
- プログラミングHaskell (オーム社) Graham Hutton(著) 山本 和彦(翻訳)
練習問題 1.
コード(BBEdit, Emacs)
ParseP2.hs
{-# OPTIONS -Wall -Werror #-}
module ParseP2 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
-- 解析状態の型、ParseState型を定義
-- (stringは未処理の文字列、numは解析結果の一部となる、中間的な値の数値)
data ParseState = ParseState {string :: String,
num :: Int} deriving (Show)
parseP2 :: String -> Maybe GreymapP2
parseP2 s =
matchHeader "P2" s >>?
\s1 -> skipSpace (ParseState s1 0) >>?
(parseNat . string) >>?
skipSpace >>?
\(ParseState s2 width) -> parseNat s2 >>?
skipSpace >>?
\(ParseState s3 height) -> parseNat s3 >>?
over >>?
skipSpace >>?
\(ParseState s4 maxGrey) ->
parseInts (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 :: ParseState -> Maybe ParseState
skipSpace initState = Just (ParseState (dropWhile isSpace (string initState))
(num initState))
over :: ParseState -> Maybe ParseState
over initState | num initState > 255 = Nothing
| otherwise = Just initState
matchHeader :: String -> String -> Maybe String
matchHeader prefix str
| prefix `isPrefixOf` str =
Just (drop (length prefix) str)
| otherwise = Nothing
-- nat(natural number)
parseNat :: String -> Maybe ParseState
parseNat s = let (n, str) = span isDigit s
in if null n
then Nothing
else Just (ParseState str (read n :: Int))
parseInts :: Int -> String -> Maybe [Int]
parseInts 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 (m, str) = span isDigit (tail s)
in if null m
then Nothing
else iter (n - 1) str (ns ++ [read m :: Int])
| otherwise = Nothing
入出力結果(Terminal, runghc)
$ runghc ParseP2 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] $
このコードは、中間的な値の数値の扱い方に問題、誤解があることに気がついたので、次回に修正することに。
0 コメント:
コメントを投稿