2008-05-01から1ヶ月間の記事一覧

Stateモナドを考える(その4)

モナド則の確認 一個一個モナド則を確認してみます.Haskellの表記としては間違っているかもしれませんが,意味が通じるように書きます. -- return a >>= k == k a runState (return a) s = (a,s) runState (m >>= k) s = let (a, s') = runState m s in ru…

Stateモナドを考える(その3)

Stateモナドがモナドであることの定義は以下の通りです. -- Monadクラスとクラスメソッド class Monad m where (>>=) :: forall a b. m a -> (a -> m b) -> m b return :: a -> m a -- State sをMonadクラスのインスタンスにする instance Monad (State s) …

Stateモナドを考える(その2)

分からないときは定義をみるのはお約束.ということでControl.Monad.Stateをみます. module Control.Monad.State ( module Control.Monad.State.Lazy ) where import Control.Monad.State.Lazy はうっ・・別の呼んでますね.本体はControl.Monad.State.Lazy…

Stateモナドを考える(その1)

sampou.orgのhttp://www.sampou.org/cgi-bin/haskell.cgi?Memoiseをほとんど丸写しして,Stateモナドを使ってフィボナッチを書いてみました. import Control.Monad.State rfib :: Integer -> Integer rfib 0 = 0 rfib 1 = 1 rfib n = rfib (n-1) + rfib (n-…

Stateモナドがすっきり分からない

Project Euler Problem 14に関連して,どうにもStateモナドがしっくりこない.キャッシュを持ちまわすため方法がすっきりしない.たぶんモナドの理解が甘いのであろう.とりあえずお約束のフィボナッチ数列からスタートでしょう.

Problem 14 (その3:答えはでた)

なぜスタックがあふれるのか.答えは自明で何でも溜め込むからなんですよね.1000000までの各整数に対してコラッツ列の長さを求めて,最大のものを求めると1000000個の要素のリストを扱わないといけないのは当たり前で,当然あふれても仕方がないです.キャ…

Problem 14 (その2:未解決)

わからない・・・どうやっても1000000で処理すると *** Exception: stack overflow がでるか,異常な時間がかかってやってられない・・・むむーー.前のコード {- Project Euler Problem 14 e14.hs -} collatz::Integer -> [Integer] collatz n | n == 1 = […

Problem 14

The following iterative sequence is defined for the set of positive integers:n n/2 (n is even) n 3n + 1 (n is odd)Using the rule above and starting with 13, we generate the following sequence: 13 40 20 10 5 16 8 4 2 1It can be seen that th…

Problem 13

Work out the first ten digits of the sum of the following one-hundred 50-digit numbers. 37107287533902102798797998220837590246510135740250 46376937677490009712648124896970078050417018260538 7432498619952474105947423330951305812372661730962…

Problem 12

The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...Let us list the factors o…

Problem 11

In the grid below, four numbers along a diagonal line have been marked in red.08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 81 49 31 73 55 79 14 29 93 71 40 67 53 88…

VMware WS 5.5.6にUbuntu 8.0.4を入れてみた

レポジトリが応答しない なぜかレポジトリが落ちててemacsが入りませんでしたが,これはレポジトリを変えればOKでした.http://www.ubuntulinux.jp/switch-archive-mirror を参照. 「マウスのホイール(wheel)が動かない」「マウスがシームレスに動かない」…