right now I have an error that says parse error on input ‘|’ that refers to the '|' before the if statement. I am also not sure if I can use let in guards like the code below. The code below is an example to my problem, please help to correct my mistakes, thank you in advance!
func x y
| let
sum = x + y
mult = x * y
| if sum == 3
then do
sum+=1
else if mult == 5
then do
mult -=1
In fact, Haskell2010 allows the let
expression to appear in the guard. See the report here. |let decls
introduces the names defined in decls
to the environment.
For your case, we can write
fun x y
| let sum = x + y, sum == 3 = Just (sum + 1)
| let mult = x * y, mult == 5 = Just (mult - 1)
| otherwise = Nothing
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With