简体   繁体   中英

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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