Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign Lists/Tuples with do notation

I have a function f that goes something like this:

f = do
      x1 <- g
      x2 <- g
      x3 <- g
      ...
      xn <- g
      return [x1,x2,x3,..., xn] --or (x1,x2,x3,..., xn)

This requires many lines of code and I have a feeling this can be done prettier. I would like to know if there is a way to do something like this:

f = do
      [x,y,z] <- [g,g,g]
      return [x,y,z]
like image 883
BBB Avatar asked Dec 05 '22 08:12

BBB


1 Answers

Use sequence and replicate:

f = do
    xs <- sequence $ replicate n g
    return xs
like image 126
Zeta Avatar answered Jan 07 '23 20:01

Zeta