从列表中提取一个随机元素(并将其删除),学习monad



我在Haskell中用Haskell维基解密和我自己的新手实验学习monad

作为我的第一步,我使用伟大的HaTeX库(Daniel Díaz)将这本书中与monad相关的部分复制到我自己的PDF中,同时轻松阅读这些硬文本(因为内容很难)。

下面是我第一次尝试实践我学到的东西:一个函数的7个版本,它接受一个列表并从该列表中返回一个随机元素

现在我的主要问题是,我应该使用monadtransformers来进行"移除元素"部分吗?我读过它们被用来混合monad,我知道我将使用State monad和List monad

除此之外,对我的代码的任何评论都将被感激地接收(风格是最多的,因为monad是关于用风格写作的?:p)

import System.Random
import Control.Monad.Trans.State
import Control.Monad

------------------------引物意图消除随机性------------------------

extractR' :: [a] -> a
extractR' xs = xs !! randomPos
  where
    n         = length xs
    randomPos = fst pareja
    pareja    = randomR (0,n-1) stdGen
    stdGen    = mkStdGen 0

-- extractR' siempre devuelve la misma cosa, al llamarla sobre la misma
-- lista   (por tanto NO SIRVE, NO ES ALEATORIO)
-- EJ:
-- *Deck> extractR' [1..100]
-- 46
-- (0.00 secs, 0 bytes)
-- *Deck> extractR' [1..100]
-- 46
-- (0.02 secs, 0 bytes)
-- *Deck> extractR' [1..100]
-- 46
-- (0.00 secs, 0 bytes)

------------------------SEGUNDO意图消除随机性------------------------

stdGen = mkStdGen 0

extractR'' :: StdGen -> [a] -> (a, StdGen)
extractR'' gen xs = (xs !! randPos , newGen)
  where 
    n = length xs
    (randPos, newGen) = randomR (0,n-1) gen

-- esta version permite arrastrar el RNGenerator, pero es incomodisima de usar:
-- *Deck> extractR'' stdGen [1..100]
-- (46,40014 40692)
-- (0.00 secs, 0 bytes)
-- *Deck> let x = snd (extractR'' stdGen [1..100]) in extractR'' x [1..100]
-- (56,1601120196 1655838864)
-- (0.00 secs, 0 bytes)

------------------------TERCER意图消除随机性-------------------------

extractR''' :: [a] -> State StdGen a
extractR''' xs = state sol
  where
    n   = length xs
    sol = s -> ( xs !! (randPos s) ,  newGen s)
    randPos st = fst $ randomR (0,n-1) st
    newGen st  = snd $ randomR (0,n-1) st
-- extractR''' xs = 
--   state ( s -> 
--     ( xs !! (fst $ randomR (0, (length xs)-1) s ) 
--     , 
--     snd $ randomR (0, (length xs)-1) s ) )
-- pruebas en pantalla:
-- *Main> runState (extractR ['a'..'z']) (mkStdGen 0)
-- ('d',40014 40692)
-- (0.00 secs, 0 bytes)
-- *Main> runState (extractR ['a'..'z']) (mkStdGen 0)
-- ('d',40014 40692)
-- (0.00 secs, 0 bytes)
-- *Main> runState (extractR ['a'..'z']) (mkStdGen 7854)
-- ('n',314309970 40692)
-- (0.00 secs, 0 bytes)
-- works well, but the code is bad (non-monadic)
-- but we are in the State monad, so the following works:
extractR2 :: [a] -> State StdGen (a,a)
extractR2 xs = liftM2 (,) (extractR''' xs) (extractR''' xs)

------------------------CUARTO意图消除随机性-------------------------

extractR'''' :: [a] -> State StdGen a
extractR'''' xs = pr1 >>= f
  where
    n   = length xs
    pr1 = state $ randomR (0,n-1)
    f   = k -> state ( s -> (xs !! k , s) )

-- monadic code, se actualiza 1 vez el StdGen

------------------------五次去随机性-------------------------

extractR_ :: [a] -> State StdGen a
extractR_ xs = pr1 >>= f
  where
    n   = length xs
    pr1 = state $ randomR (0,n-1)
    f   = k -> state ( s -> (xs !! k , g s) )  
    g   = stdG -> snd $ next stdG

-- monadic code, se actualiza 2 veces el StdGen

------------------------去随机性--------------------------

extractR_' :: [a] -> State StdGen a
extractR_' xs = 
  do
    generator <- get              -- get = state $ st -> (st,st)
    let n = length xs
    let (k, newGen) = randomR (0,n-1) generator
    put newGen
    return (xs!!k)

-- traduccion del codigo:
extractR_'' xs = 
  get >>= 
    generator -> let n=length xs in 
      ( let (k, newGen)=randomR (0,n-1) generator in 
        (put newGen >> return (xs!!k) ) )

-- *Main> :t extractR_''
-- extractR_'' :: (RandomGen t, Monad m) => [b] -> StateT t m b
-- *Main> :t extractR_
-- extractR_ :: [a] -> State StdGen a

-- ======o     o======
--    ___________
--   |___________|    -------------------------------------------------------------------------------
--    |  /  /|     ------------------------        VERSION FINAL          ------------------------
--    |_/__/__|     -------------------------------------------------------------------------------
--   |___________| 

 --    ??????????????????? (dont know which to choose)

顺便说一句,我这样做是为了完成我的数学学位,之后我将通过Haskell:D(我是在追求奇迹吗?)

谋生

我应该使用monadtransformers进行"删除元素"部分吗?我读过它们被用来混合monad,我知道我将使用State monad和List monad

但是你没有使用列表monad!你只是在使用列表。如果说您使用的是列表monad,则意味着您使用的是专门用于列表的(>>=)return(或类似的专门用于列表中的Monad多态函数),而您没有这样做。所以我认为使用State而不使用任何变压器是完全合理的。

除此之外,对我的代码的任何评论都将被感激地接收(风格是最多的,因为monad是关于用风格写作的?:p)

我最喜欢你的第四次尝试,提醒一下:

extractR'''' :: [a] -> State StdGen a
extractR'''' xs = pr1 >>= f
  where
    n   = length xs
    pr1 = state $ randomR (0,n-1)
    f   = k -> state ( s -> (xs !! k , s) )

我唯一想做的调整是,我觉得你的f有点太复杂了。由于您根本没有修改状态,因此不需要使用State特定的函数;可以写

    f   = k -> return (xs !! k)

相反。然后可以应用monad定律,即:

m >>= (x -> return (f x)) = fmap f m

我希望看到的最终实现是:

extractR :: [a] -> State StdGen a
extractR xs = fmap (xs!!) pr1
  where
    n   = length xs
    pr1 = state $ randomR (0,n-1)

(当然,人们可以用其他几种方式拼写fmap,在这种情况下,它们主要在美学上有所不同,如(<$>)liftAliftM。)

您可能需要查看MonadRandom软件包。

import Control.Monad.Random
extractR :: MonadRandom m => [a] -> m a
extractR xs = (xs !!) <$> getRandomR (0, length xs - 1)

IORandT g m满足了MonadRandom约束,它们本质上是一个基于StateT StdGen的monad,可用于纯代码。

此外,该库定义了uniform,它相当于您的extractR(尽管它的实现相当复杂)。

最新更新