2008-12-21
■ Monad,Functor,Applicative 
- <*> とap
- pureとreturn
- fmapと<$>とliftM
- *> と >>
って全部一緒?
だとすると、
たとえば、
do v1 <- proc1
v2 <- proc2
return (v1, v2)
という処理があるとしよう。これはこう書ける。
pure (,) <*> proc1 <*> proc2
すげースッキリする。さらに、先頭が pure の場合にはまた別の演算子もあり、
(,) <$> proc1 <*> proc2
と書けばよい。ふーむ。いろいろ書けそうだ!
http://haskell.g.hatena.ne.jp/jmk/20061130
liftM2 (,) proc1 proc2でもいいのかな。
■ Applicativeを使ってみる。 
テキスト正規化トラバース1回バージョン(http://ja.doukaku.org/comment/8234/)をApplicativeとReaderモナドで書き直してみる。
import Control.Monad.Reader import Control.Applicative test = "hoge\nPOPOPOPOPO\n\nPINGPONG\n" padding' :: String->Int->Int->Reader Char (Int,String) padding' [] i m = return (0,[]) padding' ('\n':xs) i m = do (cMax,cStr) <- padding' xs 0 (max i m) padStr <- padding'' ((max cMax m) -i) return (max m cMax,padStr++cStr) padding' (x:xs) i m = (\(cMax,cStr)->(max m cMax,x:cStr)) <$> padding' xs (i+1) (max i m) padding'' :: Int->Reader Char String padding'' n = (++ "\n").replicate n <$> ask padding text ch = snd $ runReader (padding' text 0 0) ch
■ はじめて使うjoin 
do v1 <- proc1
v2 <- proc2
finalProc v1 v2
な場合にはうまくないな。
http://haskell.g.hatena.ne.jp/jmk/20061130
join使えばいけるようです。
import Control.Monad import Control.Applicative import Data.Char main = join $ printLines <$> getLine <*> getLine printLines a b = mapM_ (putStrLn.map toUpper) [a,b]
あるいは
main = join $ liftM2 printLines getLine getLine
てな感じで。
haskell-jp [1-150]
IOモナドについて
Q http://www.sampou.org/cgi-bin/w3ml.cgi/haskell-jp/msg/53
A http://www.sampou.org/cgi-bin/w3ml.cgi/haskell-jp/msg/54
テスト手法について
論点の整理 http://www.sampou.org/cgi-bin/w3ml.cgi/haskell-jp/msg/102
QuickCheck+Darcs,undefinedを使う方法
http://www.sampou.org/cgi-bin/w3ml.cgi/haskell-jp/msg/128
関連エントリ
- http://madscientist.jp/~ikegami/diary/20070706.html#p02:titlke
- Inemuri nezumi diary(2007-07-20)
- Inemuri nezumi diary(2007-08-22)
- Inemuri nezumi diary(2007-08-23)
- Inemuri nezumi diary(2007-08-24)
GHC 6.8.1
http://www.sampou.org/cgi-bin/w3ml.cgi/haskell-jp/msg/138
- Haskell Program Coverage 6.6.?Observing Code Coverage
- GHCi Debbuger 3.5.?The GHCi Debugger
Flymake-Haskell
http://www.sampou.org/cgi-bin/w3ml.cgi/haskell-jp/msg/140
http://madscientist.jp/~ikegami/diary/20071108.html#p01
http://www.emacswiki.org/cgi-bin/emacs/FlymakeHaskell
HaRe
http://www.sampou.org/cgi-bin/w3ml.cgi/haskell-jp/msg/106
Haskell用のリファクタリングツール
http://www.cs.kent.ac.uk/projects/refactor-fp/hare.html
do記法で再帰的モナド
http://www.sampou.org/cgi-bin/w3ml.cgi/haskell-jp/msg/143
http://www.sampou.org/cgi-bin/w3ml.cgi/haskell-jp/msg/144
- Control.Monad.Fixのmfixを使うか Rubyのある風景 - Recursive Monads
- GHCの拡張であるmdo記法を使う。http://www.kotha.net/ghcguide_ja/latest/syntax-extns.html#mdo-notation
http://www.sampou.org/cgi-bin/w3ml.cgi/haskell-jp/msg/146
相互参照がある場合は
http://www.sampou.org/cgi-bin/w3ml.cgi/haskell-jp/msg/147
遅延パターンを使えばいい
■ その他のツール 
HLintは、Haskell Programの一般的な間違いを見つけだすツール
$ hlint darcs-2.1.2 darcs-2.1.2\src\CommandLine.lhs:46:1: Use a string literal Found: [' ', '\t', '"', '%'] Why not: " \t\"%" darcs-2.1.2\src\CommandLine.lhs:49:1: Eta reduce Found: quotedArg ftable = between (char '"') (char '"') $ quoteContent ftable Why not: quotedArg = between (char '"') (char '"') . quoteContent darcs-2.1.2\src\CommandLine.lhs:94:1: Use concatMap Found: concat $ map escapeC s Why not: concatMap escapeC s darcs-2.1.2\src\CommandLine.lhs:103:1: Use fewer brackets Found: ftable ++ (map (\ (c, x) -> (toUpper c, urlEncode x)) ftable) Why not: ftable ++ map (\ (c, x) -> (toUpper c, urlEncode x)) ftable darcs-2.1.2\src\Darcs\Patch\ReadMonads.hs:61:29: Use const Found: \ _ -> Nothing Why not: const NothingNeil Mitchell - HLint
HAT (Haskell Tracer)
4: dimagekidimageki Hat
Hat - the Haskell Tracer http://www.haskell.org/hat/
というのがあります。
Hat をインストールする
洗練された Linux distribution を使えば簡単ですが、そうでないひとは
1. ghc http://www.haskell.org/ghc/ をインストール
2. hmake http://www.cs.york.ac.uk/fp/hmake/ をインストール
3. hat をインストール
の手順で導入する必要があります。
hmake は ghc でないコンパイラもサポートしていますが、hat を使うなら
ghc で十分だと思われます。
hat 2.04 の configure にはバグがあります。もしもインストールに失敗したら
* lib/YOUR_PLATFORM/config の GHCSYM= の行を編集
o 余計な改行をとりのぞき、 GHCSYM=604 にする
* src/hatlib/Makefile の CPPFLAGS を編集
o 該当行の ghcsym を cat している部分を 604 を代入することに変える
ことで、成功するかもしれません。
Hat を使う
1. ソースを書く
1. トレースは main 実行時に作られるので、中身を見たい関数を main で評価する
2. 実行ファイルを作る
1. hmake -hat progname
2. progname はファイル名から.hs をのぞいた名前を指定すること
3. プログラム progname ができあがるので実行
3. hat-observe progname
hat-observe の使い方は hat のページにマニュアルがあります。
ためしに square(3+4) の例をやってみると次のようになります。
-- Sample.hs
main = do print (show foo)
return ()
foo :: Int
foo = square (3+4)
square :: Int -> Int
square x = x * x
% hmake -hat Sample
% ./Sample
% hat-observe Sample
hat-observe 2.04 (:? for help, :q to quit)
hat-observe> square
square 7 = 49
hat-observe>
hat のページにパターンマッチの展開の具体例が載っています。
Debug.Traceの使い方
Pharmd2552012/01/01 05:47 Hello! eeeadck interesting eeeadck site! I'm really like it! Very, very eeeadck good!
Pharme2512012/01/01 05:47Very nice site! <a href="http://oieapxy.com/tyxqqt/1.html">cheap viagra</a>
Pharma7482012/01/01 05:48Very nice site! [url=http://oieapxy.com/tyxqqt/2.html]cheap cialis[/url]
Pharme5482012/01/01 05:48Very nice site! cheap cialis http://oieapxy.com/tyxqqt/4.html
Pharma9172012/01/01 05:49Very nice site!
Pharmf9682012/01/26 00:21 Hello! bffeecc interesting bffeecc site! I'm really like it! Very, very bffeecc good!
Pharme822012/01/26 00:22Very nice site! <a href="http://apxoiey.com/aoarvx/1.html">cheap viagra</a>
Pharmd372012/01/26 00:22Very nice site! [url=http://apxoiey.com/aoarvx/2.html]cheap cialis[/url]
Pharme8742012/01/26 00:23Very nice site!