English 中文(简体)
我(受命)执行它有什么错误?
原标题:What s wrong with my (attempted) implementation of iterateM?

我愿履行一项职能,即它的作用。

iterateM :: Monad m => (a -> m a) -> a -> [m a]

然而,我第一次撰写这一职务:

iterateM f x = f x >>= (x  -> return x  : iterateM f x )

我会发现错误:

Could not deduce (m ~ [])
from the context (Monad m)
  bound by the type signature for
             iterateM :: Monad m => (a -> m a) -> a -> [m a]
  at main.hs:3:1-57
  `m  is a rigid type variable bound by
      the type signature for
        iterateM :: Monad m => (a -> m a) -> a -> [m a]
      at main.hs:3:1
Expected type: [a]
  Actual type: m a
In the return type of a call of `f 
In the first argument of `(>>=) , namely `f x 
In the expression: f x >>= ( x  -> return x  : iterateM f x )

如果我删除我的类型,格西兹告诉我,我的职能类型是:

iterateM :: Monad m => (a -> [a]) -> a -> [m a]

我在这里失踪了吗?

最佳回答

我从您的签名中收集的东西:

iterateM :: (Monad m) => (a -> m a) -> a -> [m a]

<代码>nth contentiterateM f x is an action that le fns>m. 这非常接近iterate,我怀疑我们能够按此执行。

iterate :: (b -> b) -> b -> [b]

给我们一份<代码>b清单,我们希望有一份<代码>m as,因此,我怀疑<代码>b = m a。

iterate :: (m a -> m a) -> m a -> [m a]

我们现在需要改变<代码>f a-> m a into some of category m a -> m a. 幸运的是,也就是约束的定义:

(=<<) :: (Monad m) => (a -> m b) -> (m a -> m b)

因此:

f -> iterate (f =<<) :: (a -> m a) -> m a -> [m a]

为了将我们的初始<代码>x * a输入到所希望的m a,我们可以使用return<>code>:

return :: (Monad m) => a -> m a

因此:

iterateM f x = iterate (f =<<) (return x)

免去信道。

问题回答

您对条约的保留 M正在迫使它被列入僧.名单。 你们需要采取行动,并恢复结果。

Try:

iterateM f x = do
      x  <- f x
      xs <- iterateM f x 
      return $ x :xs




相关问题
Euler Problem in Haskell -- Can Someone Spot My Error

I m trying my hand at Euler Problem 4 in Haskell. It asks for that largest palindrome formed by multiplying two three-digit numbers. The problem was simple enough, and I thought my Haskell-fu was up ...

How does foldr work?

Can anybody explain how does foldr work? Take these examples: Prelude> foldr (-) 54 [10, 11] 53 Prelude> foldr (x y -> (x+y)/2) 54 [12, 4, 10, 6] 12.0 I am confused about these executions....

Efficient queue in Haskell

How can I efficiently implement a list data structure where I can have 2 views to the head and end of the list, that always point to a head a tail of a list without expensive calls to reverse. i.e: ...

Problem detecting cyclic numbers in Haskell

I am doing problem 61 at project Euler and came up with the following code (to test the case they give): p3 n = n*(n+1) `div` 2 p4 n = n*n p5 n = n*(3*n -1) `div` 2 p6 n = n*(2*n -1) p7 n = n*(5*n -3)...

Ways to get the middle of a list in Haskell?

I ve just started learning about Functional Programming, using Haskel. I m slowly getting through Erik Meijer s lectures on Channel 9 (I ve watched the first 4 so far) and in the 4th video Erik ...

haskell grouping problem

group :: Ord a => [(a, [b])] -> [(a, [b])] I want to look up all pairs that have the same fst, and merge them, by appending all the list of bs together where they have the same a and discarding ...

Closest equivalent to subprocess.communicate in Haskell

I want to do a popen() / python s subprocess.communicate from Haskell - start a program, give it stdin, and get its stdout/stderr. What s the most direct / Haskellish way to do this?