オセロのプログラムを書いたときのメモ
オセロのプログラムをHaskellで作成。こんな修正したよというメモ。
なかなか作法がわからないからね。
if を使わずガードで表記
- rowCheck x y cb =
- if x < 0 then
- True
- else
+ rowCheck x y cb
+ | x == -1 = True
+ | otherwise =
こっちのほうがきれいにかけますね。
List関係いろいろ
リファレンスをながめていて色々できることに気がついた。- if and [getPiece x y ob == Empty, length revPos > 0] then単純な話だけどライブラリは使おう。
+ if and [getPiece x y ob == Empty, not $ null revPos ] then
- foldl (++) "" [ob2Line (7-y) ob | y <- [0..7]]foldlでやっているのが変だった。
- ++ "/01234567\n"
+ concat ([ob2Line (7-y) ob | y <- [0..7]] ++ ["/01234567\n"])
+ doReverse piece ob pos =自分で再帰で書いていたのを工夫したらfoldlでできるようになった。
+ foldl (\ob p -> (putOnPiece (fst p) (snd p) piece ob)) ob pos
- doReverse piece ob [] = ob
- doReverse piece ob (p:pos) =
- doReverse piece (putOnPiece (fst p) (snd p) piece ob) pos
- concatMap (\y -> ob2Line (7-y) ob) [0..7] ++ "/01234567\n"これもライブラリを使ったほうが自然ですよと。
+ concatMap (ob2Line ob) (reverse [0..7]) ++ "/01234567\n"
- initBoard =これはちょっと迷うところではあるけれど。
- Map.fromList [( (p2int 3 4), White), ((p2int 4 4), Black),
- ((p2int 3 3), Black), ((p2int 4 3), White)
- ]
-
+ initBoard =
+ let ob = Map.empty in
+ foldl (\ob p -> putOnPiece (fst (fst p)) (snd (fst p)) (snd p) ob)
+ ob [((3,4), White), ((4,4), Black),
+ ((3,3), Black), ((4,3), White)]
do記法の意味がちょっとわかった
catch (readIO line :: IO (Int,Int))syntax sugar という話。>> がずらずら並ぶときには do を使えば消える
- (\e -> return (e::SomeException) >>
- putStr "Input again!: " >>
- hFlush stdout >>
- readPos)
+ (\e -> do
+ return (e::SomeException)
+ putStr "Input again!: "
+ hFlush stdout
+ readPos)

Debian QA
Facebook (taniguchi.takaki)
Twitter (@takaki_t)