|
|
||
mainは予約語ではない。
ghc等でコンパイルする際に主関数であることを明示するために使用する
at 勉強会
処理系が使用しているだけ
mainの型は IO a
head.hsをghciで対話的に動かしてみる。
Ok, modules loaded: Main. *Main> main 1 1 2 2 3 3 ...省略... 8 8 9 9 10 10 *Main>
入力するたびに入力行が表示され、10回表示した後に処理が終了
遅延評価のせい?
at 勉強会
http://www.fobj.com/hisa/diary/20060604.html#p02を参照
2つの処理が似ているので、delimit関数を引数にとるcount関数を
作成
countBytesはidentity関数なので、もっと良い書き方があるかもしれない
main = do cs <- getContents
print $ count cs countBytes
count cs delimit = length $ delimit cs -- 共通
countBytes cs = cs
at 勉強会
バイト数がこれでカウントできるのは、2バイト文字が文字列中で2つの要素に分割されるから
main = do cs <- getContents
print $ count cs words
count cs delimit = length $ delimit cs -- 共通
ちょっとズルして無名関数を使って、countBytes関数を消してみる
main = do cs <- getContents
print $ count cs (\x -> x)
count cs delimite = length $ delimite cs
main = do cs <- getContents
print $ count cs id
count cs delimite = length $ delimite cs
これ以上は無理か
main = getContents >>= print . length
sshi emacs at Linux/haskell-mode
to2y xyzzy at Win2K/haskell-mode
mainの考察 2人(10人中)
フェアネス重視 = 強い型付け
第3章 sshi 6/18
これくらいの長さのコードだと(超範囲外だけど)、関数合成の多用であるポイントフリースタイル(p.209)とdo式の書き換え(p.278)を使うとものすごく短くなるよ。
#手探りでコメントつけてみた