How to use>> = to replace do and <- in this code?

My question is how to work with monads. I have the following code:

import System.Random
import Data.Random.Normal
import Graphics.EasyPlot
import Data.List
import Data.Function 

random' mean randomGen = zip x y 
    where                
       x = normals' (mean, 0.2) randomGen :: [Float]
       y = normals' (mean, 0.2) randomGen :: [Float]

randomW1 randomGen = [[x,y] | (x,y) <- random' 1.0 randomGen]

main = do  
    randomGen <- getStdGen
    let w1 = take 50 (randomW1 randomGen)
    print w1 

and it works great. However, I think that its limitation is to connect the output getStdGenoutside randomW1and thought that I could connect getStdGenmore directly with randomW1writing

w1 = take 50 (randomW1 =<< getStdGen) 

I suppose I used texture structures for >>=either the =<<“pipe” and replaced using doand <-. When I do as I suggest, I find that he

Couldn't match type ‘IO’ with ‘[]’
Expected type: [StdGen]
  Actual type: IO StdGen  

Is there a way to use >>=a replacement doand <-in this code?

+4
1
main = do  
    w1 <- getStdGen >>= (return . take 50 . randomW1)
    print w1 

( )

, >>= (return . f) fmap f :

main = do  
    w1 <- (take 50 . randomW1) `fmap` getStdGen
    -- or: w1 <- take 50 . randomW1 <$> getStdGen
    print w1 

<-, :

main = print . take 50 . randomW1 =<< getStdGen

, . :

main = do  
    randomGen <- getStdGen
    let w1 = take 50 (randomW1 randomGen)
    print w1 

Inline w1:

main = do  
    randomGen <- getStdGen
    print (take 50 (randomW1 randomGen))

Desugar do x <- m ; e m >>= \x -> e. do.

main = getStdGen >>= \randomGen -> print (take 50 (randomW1 randomGen))

:

main = getStdGen >>= print . take 50 . randomW1
+7

Source: https://habr.com/ru/post/1609528/


All Articles