14122009 game
CHAOS Lv.46
パーティーは、破壊神トナテイウ、地母神ハリティ、邪龍キングー
ステータスは揃えとかないとキツイのと、マハンマ耐性つけたパーティを如何に用意するかだった。回復はちまちまやってもしょうがないので、宝玉輪で一気にやってあとはひたすらアギ系でcoop
14122009 game
CHAOS Lv.46
パーティーは、破壊神トナテイウ、地母神ハリティ、邪龍キングー
ステータスは揃えとかないとキツイのと、マハンマ耐性つけたパーティを如何に用意するかだった。回復はちまちまやってもしょうがないので、宝玉輪で一気にやってあとはひたすらアギ系でcoop
13122009 Haskell
Functorのあたりがついてくのがきびしくなった。
Real World Haskell―実戦で学ぶ関数型言語プログラミングもう一度丁寧に読み直す。
13122009 Haskell
fromIntegralは型を変換してNum型に
Prelude> let a = 2 :: Int
Prelude> :t a
a :: Int
Prelude> :t (fromIntegral a)
(fromIntegral a) :: (Num b) => b
Num型なので、Fractionalの和も求まる
Prelude> (fromIntegral a) + 3.0
5.0
13122009 game
ちょっと戦術が必要な感じ。
力技でなくて、戦略的に攻略していくための悪魔合体が楽しい。もうすぐ30時間程のプレイ時間になる。
久々に楽しいゲームだ。年末年始の暇つぶしにもよさげ。
09122009 Haskell
例外処理をやるためにhandleとbracketがある。
Prelude> :m +Control.OldException
Prelude Control.OldException> :t handle
handle :: (Exception -> IO a) -> IO a -> IO a
Prelude Control.OldException> :t bracket
bracket :: IO a -> (a -> IO b) -> (a -> IO c) -> IO c
handleは最初の引数に処理が失敗した時の例外ハンドラ。本ではファイルのサイズを返す関数を書くときに使っていて、openに失敗したらNothingを返すようなアクションを定義してた。
bracketは最初のアクションでリソース獲得、二番目のアクションがリソース開放、最後のアクションがリソース獲得から開放までにはしるアクション。pythonでいうところのtry-except-finallyみたいなものか。
RWHではhandleとbracketを組み合わせて
getFileSize path = handle (\_ -> return Nothing) $
bracket (openFile path ReadMode) hClose $ \h -> do
size <- hFileSize h
return (Just size)
ってなってんだけど、もしhandleなしでbracketのopenが失敗して例外が返ったらどうなんのとか?基本的にhandleとbracketは組み合わせて使うものなのか?
Real World Haskell―実戦で学ぶ関数型言語プログラミング08122009 Haskell
9章のforMの練習
forM :: (Monad m) => [a] -> (a -> m b) -> m [b]
リストと、関数をとってリストのモナドを返す
module RecursiveContents (getRecursiceContents) where
import Control.Monad (forM)
import System.Directory (doesDirectoryExist, getDirectoryContents)
import System.FilePath ((</>))
import Text.Regex.Posix ((=~))
getRecursiceContents :: FilePath -> IO [FilePath]
getRecursiceContents topdir = do
names <- getDirectoryContents topdir
let properNames = filter (`notElem` [".", ".."]) names
paths <- forM properNames $ \name -> do
let path = topdir </> name
isDirectory <- doesDirectoryExist path
if isDirectory
then getRecursiceContents path
else
if path =~ ".+~$"
then return [path]
else return []
return (concat paths)
実行
$ ghci RecursiveContents.hs
*RecursiveContents> getRecursiceContents "/Users/kzfm/perl"
["/Users/kzfm/perl/hori.pl~","/Users/kzfm/perl/Moosp/lib/Moosp/Function \
/Defun.pm~","/Users/kzfm/perl/Moosp/t/17-function-defun.t~"]
Real World Haskell―実戦で学ぶ関数型言語プログラミングdoesDirectoryExist関数はアクションで返り値はIO Boolのため、<-でBoolにする必要がある。
06122009 work
タイトルは微妙だが、領収書に関するアレコレ。
06122009 Haskell
RWH8章のメモ
Haskellの正規表現のための演算子は=~でperlと一緒だ。
Prelude> :m +Text.Regex.Posix
Prelude Text.Regex.Posix> "Haskell" =~ "^.+ll$" :: Bool
Loading package syb ... linking ... done.
Loading package array-0.2.0.0 ... linking ... done.
Loading package bytestring-0.9.1.4 ... linking ... done.
Loading package regex-base-0.72.0.2 ... linking ... done.
Loading package regex-posix-0.72.0.3 ... linking ... done.
True
さらに型の指定を変えると、それに合わせた値を返してくる。(Int,Int)とか。=~の型が気になるので:tして後悔した。
Prelude Text.Regex.Posix> :t (=~)
(=~)
:: (Text.Regex.Base.RegexLike.RegexMaker
Regex CompOption ExecOption source,
Text.Regex.Base.RegexLike.RegexContext Regex source1 target) =>
source1 -> source -> target
ぶっちゃけ全然わからん。
Real World Haskell―実戦で学ぶ関数型言語プログラミング06122009 Haskell
抽象度を上げていく。
Real World Haskell―実戦で学ぶ関数型言語プログラミングファイルを読んで、コンテンツを大文字に変えてファイルに書き込む処理を考える。これはハンドルを開いて読み書きすればいい。
import System.IO
import Data.Char(toUpper)
main :: IO ()
main = do
inh <- openFile "input.txt" ReadMode
outh <- openFile "output.txt" WriteMode
mainloop inh outh
hClose inh
hClose outh
mainloop :: Handle -> Handle ->IO ()
mainloop inh outh =
do ineof <- hIsEOF inh
if ineof then return ()
else do inpStr <- hGetLine inh
hPutStrLn outh $ processData inpStr
mainloop inh outh
processData :: String -> String
processData = map toUpper
このmainloop関数はEOFが見つかるまでストリームを読んでいくが、これはhGetContentsで抽象化出来る
import System.IO
import Data.Char(toUpper)
main :: IO()
main = do
inh <- openFile "input.txt" ReadMode
outh <- openFile "output.txt" WriteMode
inpStr <- hGetContents inh
let result = processData inpStr
hPutStr outh result
hClose inh
hClose outh
processData :: String -> String
processData = map toUpper
さらに、さらにハンドルを開いて文字列を読み出す処理と、文字列に大して何らかの処理をしながら、ハンドルを開いてファイルに書き出すという処理もそれぞれreadFile,writeFileで抽象化できるので、結局次のようになる
import Data.Char(toUpper)
main = do
inpStr <- readFile "input.txt"
writeFile "output.txt" $ processData inpStr
processData :: String -> String
processData = map toUpper
となると、読み出すファイル名と、書き出すファイル名と、ストリームに対し作用させる関数を受け取ってよろしくやってくれる関数まであっていいと思うのだけど、それは標準ライブラリに存在するのだろうか?
convert readfile writefile (map toUpper)
みたいなの。