SICP

お知らせ

効率が悪いので「HaskellでSICP」はしばらくお休みします。

3日目 数値の型など

1.3.3 search を書いてみる。 search :: (Fractional a, Ord a) => (a -> a) -> a -> a -> a search f neg pos = let mid = average neg pos in if closeEnough neg pos then mid else case signum (f mid) of 1 -> search f neg mid -1 -> search f mid pos…

2日目

1.2.6 素数 最小の除数を求める。この通りにやるより、除数のリストを作るほうが簡単かな。 divisers :: Int -> [Int] divisers n = [ x | x <- [2 .. limit], n `mod` x == 0] where limit = floor (sqrt (fromIntegral n)) isPrime :: Int -> Bool isPrime…

遅延評価と末尾再帰

そういうことで、問題1.11 の反復的なものを Haskell で書くときはこの前書いた f n = iter 2 1 0 n where iter a b c count | count == 0 = c | otherwise = iter (a + 2 * b + 3 * c) a b (count - 1)では固定スペースで実行できなくて、こうやらないとい…

1日目

ひげぽんはp26の演習をやっているのでそこに追いつくのが目標です。 序文 読みづらい!パス 第1章 p1 - p4 「環境」 注釈の括弧が入れ子になってるところがさすがだね。 p17 sqrtをhakellで書き直したりした p20 末尾再帰 再帰的プロセスの方が自然に見える…