简体   繁体   English

do 块中的 if/then/else 中的语法在 Haskell 中如何工作

[英]How does the syntax in a if/then/else within a do block work in Haskell

I'm trying to make the folowing function:我正在尝试实现以下功能:

repcountIORIban :: IORef -> Int -> Int -> Int -> Int -> Lock -> IORef -> Lock -> Int -> Int -> IO ()
repcountIORIban count number lower modulus amountthreads lock done lock2 difference rest = do
        if rest > number
            then let extra = 1
            else let extra = 0
        if number + 1 < amountthreads
            then
                forkIO $ realcountIORIban(count lower (lower + difference + extra - 1) modulus lock done lock2)
                repcountIORIban (count (number + 1) (lower + difference + extra) modulus amountthreads lock done lock2 difference rest)
            else
                forkIO $ realcountIORIban(count lower (lower + difference + extra - 1) modulus lock done lock2)

But I can't run the program from which this function is a part of.但是我无法运行这个函数所属的程序。 It gives me the error:它给了我错误:

error: parse error on input `else'
    |
113 |             else let extra = 0
    |             ^^^^

I've got this error a lot of times withing my program but I don't know what I'm doing wrong.我的程序多次出现此错误,但我不知道自己做错了什么。

This is incorrect, you can't let after then / else and expect those let s to define bindings which are visible below.这是不正确的,您不能let after then / else并期望那些let定义下面可见的绑定。

do if rest > number
     then let extra = 1  -- wrong, needs a "do", or should be "let .. in .."
     else let extra = 0
   ... -- In any case, extra is not visible here

Try this instead试试这个

do let extra = if rest > number
               then 1
               else 0
   ...

Further, you need then do if after that you need to perform two or more actions.此外,如果then do您需要执行两个或更多操作,则需要执行。

if number + 1 < amountthreads
  then do
    something
    somethingElse
  else         -- use do here if you have two or more actions
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM