English 中文(简体)
我的摩院随机执行中非常缓慢的警卫(萨克尔)
原标题:Very slow guards in my monadic random implementation (haskell)

我试图根据数量类别撰写一份随机数字生成器执行书。 我也补充说,Monad和Monad Plus案。

什么意思是“Monad Plus”? 因为我想在这里使用警卫:

--  test.hs       --

import RandomMonad
import Control.Monad
import System.Random 

x = Rand (randomR (1 ::Integer, 3)) ::Rand StdGen Integer

y = do
 a <-x
 guard (a /=2) 
 guard (a /=1)
 return a

这里是Random Monad.hs案的内容:

-- RandomMonad.hs --

module RandomMonad where
import Control.Monad
import System.Random 
import Data.List 
data RandomGen g => Rand g a = Rand (g ->(a,g))  | RandZero

instance (Show g, RandomGen g) => Monad (Rand g)
 where
 return x = Rand (g ->(x,g))
 (RandZero)>>= _ = RandZero

 (Rand argTransformer)>>=(parametricRandom) =  Rand funTransformer 
  where 
  funTransformer g | isZero x = funTransformer g1
                   | otherwise = (getRandom x g1,getGen x g1)
   where
   x = parametricRandom val
   (val,g1) = argTransformer g
   isZero RandZero = True
   isZero _ = False

instance (Show g, RandomGen g) => MonadPlus (Rand g)
 where
 mzero = RandZero
 RandZero `mplus` x = x
 x `mplus` RandZero = x
 x `mplus` y = x 

getRandom :: RandomGen g => Rand g a ->g ->a
getRandom (Rand f) g = (fst (f g)) 
getGen :: RandomGen g => Rand g a ->g -> g
getGen (Rand f) g = snd (f g)

当我掌握了格西语译员并在指挥下调任。

getRandom y (mkStdGen 2000000000)

我可以看到我的计算机(1G)的记忆流。 它预计不会,如果我删除一名警卫,它就非常快。 为什么在这种情况下工作太缓慢?

我有什么错误?

问题回答

你对<代码>(>>=的定义无疑是错误的,但我不能指出,因为如此复杂! 我将解释为什么不能用一个例子来正确界定它。 考虑:

Rand (g -> (42,g)) >>= const mzero

我们需要删除<条码>42,因此我们需要一个<条码>。 获取货物的地点从受约束者的返回价值上,因此答案是肯定的:

Rand (g -> ...)

某些网站.负责填写(b,g)。 既然我们有42个,我们可以评价<代码>const mzero 42,发现我们有。 RandZero 但是,我们将在什么地方获得<条码>b? 页: 1 在这种例子中,这种表述没有任何类型,因为表达的类型是forall b. Rand b

mon的<代码>RandZero的目的是什么? 您只是试图制定<条码>。 StateT g Maybe 我的猜测是你。 在这种情况下,你可能更愿意执行ts。 类型:

newtype Rand g a = Rand (g -> Maybe (a, g))

如果我正确理解你的“mon”,(>>=>就没有联系。 定义

y  = do a <- do a  <- x
                guard (a  /= 2)
                return a 
        guard (a /= 1)
        return a

检查情况。 有效的是,你的追随战略只能打破最后一步,而不是整个计算。





相关问题
What to look for in performance analyzer in VS 2008

What to look for in performance analyzer in VS 2008 I am using VS Team system and got the performance wizard and reports going. What benchmarks/process do I use? There is a lot of stuff in the ...

SQL Table Size And Query Performance

We have a number of items coming in from a web service; each item containing an unknown number of properties. We are storing them in a database with the following Schema. Items - ItemID - ...

How to speed up Visual Studio 2008? Add more resources?

I m using Visual Studio 2008 (with the latest service pack) I also have ReSharper 4.5 installed. ReSharper Code analysis/ scan is turned off. OS: Windows 7 Enterprise Edition It takes me a long time ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

热门标签