2010-01-01から1ヶ月間の記事一覧
簡約(かんやく) 圏論(けんろん)
*Main> :t Right Right :: b -> Either a b *Main> :t Left Left :: a -> Either a b *Main> Right "Right" Right "Right" *Main> Left "Left" Left "Left" *Main> :t identity 1 identity 1 :: (Num t) => Parse t *Main> :t identity "foo" identity "foo"…
じゃってことで runParse関数の型を runParse :: ParseState -> Either String (a, ParseState) から runParse :: Parse a -> ParseState -> Either String (a, ParseState) に変えて:loadするとどうなるか? Prelude> :load Parse.hs [1 of 1] Compiling Ma…
試しに以下のような関数を書いてみた f x y z = x * y * zこれをghciで:loadすると *Main> :load testset.hs [1 of 1] Compiling Main ( testset.hs, interpreted ) Ok, modules loaded: Main. *Main> f 1 2 3 6 *Main> :t f f :: (Num a) => a -> a -> a ->…
テキストの内容をそのまま写経すると以下のようになる -- file : ch10/Parse.hs import qualified Data.ByteString.Lazy as L import Data.Int data ParseState = ParseState { string :: L.ByteString , offset :: Int64 } deriving (Show) newtype Parse a…
ブログでソースコードをそのまま表記したい。 http://hatenadiary.g.hatena.ne.jp/keyword/%e3%81%af%e3%81%a6%e3%81%aa%e8%a8%98%e6%b3%95%e4%b8%80%e8%a6%a7 はてな記法を使えばいいのか? スーパーpre記法らしい parse :: Parse a -> L.ByteString -> Eit…
本読んでるだけじゃ納得できない部分があるので 実際に動かしてみた。
だもんでfoldがlだろうがrだろうがあんまし関係ない。 と思う。
すこしモナドが分かって来たカモ doとか (>>=)はbindと言うそうな。
後ろで出てくるchooseと同じ考え方で行くと fからfuncに変えられるのは3箇所data Tree a = Fork (Tree a) (Tree a) | Leaf a | Nil deriving (Show)instance Monad Tree where return a = Leaf a Nil >>= f = Nil Leaf a >>= f = f a Fork u v >>= f = Fork…
今までの例の場合、最終的に2番目のパターンにマッチすればいいので instance Monad Tree where return a = Leaf a Nil >>= func = Nil Leaf a >>= func = f a Fork u v >>= func = Fork (u >>= func) (v >>= func) f 2 = Fork Nil (Leaf "Two") f 3 = Fork…
returnはある型aをモナドで包むと。(>>=)の左辺は、パターンマッチ的につかって ※パターンマッチに使うのは、モナドにした型のデータコンストラクタ。 右辺のf=関数本体で適用したい処理を書くと。 ※fって関数名は何でもいいのかも。 ※↑ってわけではないらし…
monads.ja.pdfよりdata Tree a = Fork (Tree a) (Tree a) | Leaf a | Nil deriving (Show) instance Monad Tree where return a = Leaf a Nil >>= f = Nil Leaf a >>= f = f a Fork u v >>= f = Fork (u >>= f) (v >>= f) f 2 = Fork Nil (Leaf "Two") f 3 =…
LaTexで変換したpdfがHaskell-jpのMLに流れてた。
http://www.iplab.cs.tsukuba.ac.jp/~ishihara/platex_install.htm の通りにインスコ中。 というのも、monads.ja.lhsをTexってpdfにしたいから 本体とオプションを入れる感じ。 Rに近いなぁ。
http://book.realworldhaskell.org/read/ で公開されてる。 公開してて本は売れるのかな?
既に、ジェットストリーム、MVPenを導入してるので、筆記用具には不満ないけど、衝動買いしてみた。 ホントはロザンプロデュースのナカバヤシのノートが欲しかったんだけど… 通関士試験勉強用に。
他のファイルがモジュールで定義されてないと コンパイルエラーになるメイン import 自作のなにか (個別でimportするもの)自作の何か module なんとか (exportするもの) where 本体
JSONデータの操作 renderJValueで表示形式を作成して、それを実際に表示する 汚れたcompactやprettyに分けるという考えは Lisper的にとりあえずList作れば、あとはどんな形であれ表現するだけ という考えに似てわかるんだけどそこに行き着くまでの補助関数が…
Maybe aは、NothingかJust aを生成する。 このとき、Just aのaを取り出す場合は、パターンマッチで 値を取得するcase value fo Nothing -> Nothing Just a -> aみたいな感じで。
関数合成する場合、関数fと関数gを合成するとして f . g と f $ g って同じじゃないか?と思った。 でも:infoで型を見ると Prelude> :info (.) (.) :: (b -> c) -> (a -> b) -> a -> c -- Defined in GHC.Base infixr 9 . Prelude> :info ($) ($) :: (a -> b…
Prelude> let a `plus` b = a + b Prelude> 10 `plus` 20 30 Prelude> plus 10 20 30 以前試した時は上手く行かなかったような・・・ let忘れですか、そーですか。
Prelude> let sumsum x y = x + y Prelude> sumsum 10 20 30 Prelude> let wrap = sumsum Prelude> :type wrap wrap :: Integer -> Integer -> Integer Prelude> wrap 10 20 30
elseが必須で - if 条件 then A else B - AとBは同じ型を返さないといけない。
Haskellはパラメトリック多相であり、 オブジェクト指向言語はサブタイプ多相
とりあえず読み終えてみた。 ふつうのHaskell → Real World Haskell → プログラミングHaskell また、Real World Haskellに戻ってみる。
Prelude> let f x y = x + y Prelude> f 2 3 5 Prelude> f (2 3):1:3: No instance for (Num (t -> a)) arising from the literal `2' at :1:3-5 Possible fix: add an instance declaration for (Num (t -> a)) In the first argument of `f', namely `(2 3…
Prelude> compare 2 2 EQ Prelude> compare 2 3 LT Prelude> compare 4 3 GTPrelude> :type GT GT :: Ordering Prelude> :type EQ EQ :: Ordering Prelude> :type LT LT :: OrderingPrelude> :info GT data Ordering = ... | GT -- Defined in GHC.Ordering …
Doubleの方が最適化されてるので効率がいいそうです。
時間帯によっては、鬼パネェぐらい混む。しかし、この店はすべてのレジで会計が出来る。 というか、そーゆー案内が張り出されている。てなわけで、混んでる時は文房具のレジに行くべし。