So I am just getting the hang of Haskell and got stuck with the syntax for creating nested if-else statements with Do. I am not sure, if I am supposed to use multiple Do's or if I can accomplish it all with just one. I am also not sure if I should use the return statement and if all the semicolons are necessary.
I tried adding and removing braces, return statements, semicolons, different indenting and do not know what else to try.
I get errors like " • Couldn't match expected type ‘m0 (m0 b0)’ with actual type ‘Int’ • In a stmt of a 'do' block: a <- getVal "
This is the kind of structure I am trying to build but it gives me errors. You can imagine the getVal functions as arbitrary functions, that return an integer :
func :: Int -> Int func val = do { if val < 10 then do { a <- getVal0; b <- getVal1; if (val+a+b) < 0 then return 1 else return 2} else if val > 10 then do { a <- getVal2; b <- getVal3; if (val + a +b) < 0 then return 0 else return 1} else return 99; }
An example of get val would be:
getVal :: Int
getVal = 5
So this is what I want to happen :
If val is < 10, then get the value a with 2 parameters from getVal0 and get b from getVal1 . Then add val+a+b and if that is smaller than 0, return 1, else 2
If val is > 10, then get a from getVal2 and get b from getVal3. If val+a+b <0 then return 0, otherwise 1 .
If val is neither <10 or >10 ( val = 10) then return 99
Thanks for feedback and help!
getVal0andgetVal1? But it looks like you are trying to use adoblock where it is both unnecessary and invalid.doblocks are only for computations within a monad -Intis not a monadic value so you can't use adoblock to calculate such a value.dodoesn't do what you think it does. Dittoreturnand<-. This function needs none of those.let ... in ...syntax here.donotation, etc, etc. Even trying to fix this code block depends very much on the type of the made-upgetVal1. Could you try asking just a single question, with an MCVE if you are still stuck?let, without getting into whydois inappropriate, would be sufficient. The OP commented thatgetVal0andgetVal1are functions, so we can assume that something likelet a = getVal0 parm1 parm2 in ...is the intent.