Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use let in guards on Haskell?

Tags:

haskell

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
like image 530
Saki Avatar asked Dec 11 '22 04:12

Saki


1 Answers

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
like image 162
Z-Y.L Avatar answered Dec 26 '22 12:12

Z-Y.L