Haskell IF statements

I am new to haskell, but if you do an if statement:

function a b c
     | (a+b == 0) = True
     | --etc.
     | otherwise = False

Is the second if statement the same as else if in other languages, or is it just different. I guess this is the first one since you can only have one conclusion, but I just want to make sure.

+5
source share
4 answers

The design used is called protection . Haskell checks these alternatives one by one until one condition gives True. He then evaluates the right side of this equation.

You could write well

function n 
   | n == 1 = ...
   | n == 2 = ...
   | n >= 3 = ...

thus, the types of protection are an if / elseif construct from other languages. Since otherwiseit is simply defined as True, the last

| otherwise =

, , catch-all else.

, Haskell a = if foo then 23 else 42.

+9

, , if, . , "" , ( | =), ( True). otherwise True ( "" ).

+2

else, .

otherwise True, , , .

+1

Correctly. Although you used guards, the way you expressed it is more or less identical to using if-statement. The flow of checking the conditional result to get the result will fall through the guard you wrote in the order in which they were indicated in your guard.

(a+b == 0)

Will be checked first

etc.

The second one will be checked, etc. provided that the previous condition is not true.

otherwise

The latter will be checked if the previous condition is not true.

+1
source

All Articles