English 中文(简体)
汇编错误: “ do 构造中的最后语句必须是表达式”
原标题:Compilation error: "The last statement in a do construct must be an expression"
以下是我的餐饮哲学家代码, 并产生一个编译错误, 说“ 做构造中的最后语句必须是表达 : mVar2 & lt; - 新的 mmptyMVar3; 新的 mvtymvmvl= improceration. compt import control. Vental 进口控制. Conput. movar_ 进口系统. Randomtfork * Int- & gt; forks- gt; 叉 - gt; form2; mal2 - mmvalm; mmvk=mmmvr=listal!n.
最佳回答

你的代码有很多问题

您报告的错误信息显示您可能正在混合空格和制表符。 取消标签并只使用空格 。


您大概是在写这个程序, 以便练习写哈斯凯尔程序, 而不是为了娱乐和利润而运行这个程序。 因此我们不想简单地给你一个实用的迪宁哲学家执行, 我们想帮助您写入“ 您的'/ em'执行 ” 。

我无法从你的代码得知 你如何期望它的工作。

我要集中关注最后一行:

sequence_ [ forkIO forks (mVar philosopher) ]

sequence_ * [IOa] - & gt; IO () -- -- 给 sequence {/code> a list of i/o actions, 它( 返回一个 i/ o actions) 按顺序执行每个动作。 从 < code> <... > < < > < > < > < > >, 看起来你正在尝试给它一个列表, 但只有一个元素。 这大概不是你想要的 。

forkio * IO () - & gt; IO ThreadID -- -- 给 forkio 一种i/o动作,它(返回一个i/o动作) 开始i/o动作在一条新线索中运行, 给您该线索的代号 。

这里有两个问题:

  • forks is a function, not an i/o action (it s not even a function that returns an i/o action, though you probably mean it to be)
  • you give forkIO a second argunment ((mVar philosopher)), but it only takes one argument

mVar 哲学家 本身没有任何意义: mVar * [MVar a] (这是MVar的列表,我还没有确定MVar应该包含什么类型),但你把它当作一个函数,通过 phicopher 作为参数。

此时, 灯泡会闪烁在我的头上。 您想要调用有参数 < code> mVar < / code > 和 < code > 哲学家 的 < code > forks 吗?

sequence_ [ forkIO (forks mVar philosopher) ]

我们仍然在排序一个动作 。 您是否想要将 < code> forks 和 < code> phicopher 中的每个元素相依称为 < code > / code >?

sequence_ $ map (
 -> forkIO (forks mVar n)) philosopher

我们可以简化到

mapM_ (
 -> forkIO (forks mVar n)) philosopher

这与您给定的 forks * [MVar] - & gt; [Int] 类型不匹配。 但可能是错误的, 所以您会想要在下一个修正该函数 。

问题回答

暂无回答




相关问题
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?

热门标签