2009-06-01から1ヶ月間の記事一覧

「生きるためにしなければならないこと」と「やりたいこと」

「Life is beautiful: AppBankインタビュー」より。 「生きるためにしなければならないこと」と「やりたいこと」を同じベクトルに置くことができれば人生が楽しくなる 「人のためになること」と「自分のためになること」を同じベクトルに置くことができれば…

型クラスは異なるデータ型に共通する操作を定義する。

型クラス(type class)は異なるデータ型に共通する操作を定義する。 class SayHello a where sayHello :: a -> Stringその1 Person 型 data Person = Person { name :: String, msg :: String} deriving (Eq, Ord, Show)Person 型の SayHello インスタンス in…

構造体のような型を定義してみる

data Person = Person { name :: String, age :: Int} instance Eq Person where (Person name1 age1) == (Person name2 age2) = (name1 == name2) && (age1 == age2) instance Show Person where show (Person name age) = "Person name=" ++ name ++ " age…

平衡木を使ってみる

HashTable は IO 型なので難しいから平衡木。 > :m +Data.Map > let t = empty > t > :t t --=> t :: Map k a > t --=> fromList [] 要素を挿入してみる。 > let t2 = insert "key" "value" t > let t3 = insert "1" "1234567890" t2 > t3 fromList [("1","1…

配列を使ってみる

Array を使ってみる。 > :m + Data.Array -- モジュールの import > :t listArray listArray :: (Ix i) => (i, i) -> [e] -> Array i e -- リストから配列を作ってみる。 > listArray (0,2) ["Hello","world","Hoge"] --=> array (0,2) [(0,"Hello"),(1,"wor…

Data.HashTable を使ってみる。

Haskell のお勉強 10. 種々のデータ構造を参考にData.HashTable を使ってみる。 Prelude> :m + Data.HashTable Prelude Data.HashTable> :set prompt "> " > > h <- new (==) hashString :: IO (HashTable String String) > :t h h :: HashTable String Stri…

関数の合成

> let ab a = a+10000 > :t ab ab :: (Num a) => a -> a > let cd c = c+2000000 > :t cd cd :: (Num a) => a -> aab の結果をcdの引数とする。 > cd (ab 1) 2010001cd (ab 1) の構文糖衣。 > cd $ ab 1 2010001cd と ab の関数を合成し、引数に1を渡す。 > …

Haskell の and , or は最後まで比較する

src/GHC-List.html#and List の中の論理値が全て True であるかどうかチェックするには、Falseが出現した時点で成立しない。ここでチェックをやめてもいいのだけれど Haskell は fold を使っているので最後までチェックしている。 Special folds という分類…

http://ja.wikipedia.org/wiki/Haskell の逆ポーランド記法評価器

-- 逆ポーランド記法評価器 -- calc :: String -> [Float] calc = foldl f [] . words where f (x:y:zs) "+" = y+x:zs f (x:y:zs) "-" = y-x:zs f (x:y:zs) "*" = y*x:zs f (x:y:zs) "/" = y/x:zs f xs y = read y : xs 分かりやすく変数名を変えてみる。 ca…

foldr の実行順序

Prelude> foldl (+) 10 [1 ,2 ,3 ] 16 Prelude> foldr (+) 10 [1 ,2 ,3 ] 16 Prelude> foldl (\a b -> a + b) 10 [1 ,2 ,3 ] 16 Prelude> foldr (\a b -> a + b) 10 [1 ,2 ,3 ] 16 違いが分からないので文字列の連結をさせてみる。 Prelude> foldl (\a b -> …

今日の日付を文字列で取得する。"2009/06/03"

Haskell には System.Time と Data.Time のふたつがあるようだ。 Prelude> :m + System.Time Prelude System.Time> getClockTime Wed Jun 3 09:45:04 東京 (標準時) 2009getClockTime で取得した値を CalendarTime に。 Prelude System.Time> tm <- getClock…