I'm implementing a function to perform a monadic computation N times. I've written the following code.
performN :: Monad m => Int -> m t -> m [t]
performN 0 m = return []
performN n m =
do x1<- m
x2<- if n == 1 then return [] else (m:performN (n-2) m)
return (x1:x2)
I get the following error.
:264:44: error:
• Couldn't match type ‘m’ with ‘[]’
‘m’ is a rigid type variable bound by
the type signature for:
performN :: forall (m :: * -> *) t. Monad m => Int -> m t -> m [t]
at :260:13
Expected type: [m t]
Actual type: m [t]
• In the second argument of ‘(:)’, namely ‘performN (n - 2) m’
In the expression: (m : performN (n - 2) m)
In a stmt of a 'do' block:
x2 <- if n == 1 then return [] else (m : performN (n - 2) m)
• Relevant bindings include
m :: m t (bound at :262:12)
performN :: Int -> m t -> m [t] (bound at :261:1)
I can't seem to figure out what I am doing wrong and how to fix it.
performN n-2 m
is(performN n) - (2 m)
, notperformN (n - 2) m
. After fixing that, you'll probably want to drop them:
bit and change to subtracting one. And though I suppose you're doing this just as an exercise, it'sreplicateM
. – Ry-♦