Haskell – Branch Order

Haskell

Preface

The branch statement in Haskell is quite simple and similar to most other languages. The syntax is:

if <expression> then

<Commands>

else if <expression> then

<Commands>

...

else

<Commands>

For example:

main :: IO ()
main = do    
    let v = 10    
    if v `rem` 2 == 0 then
        putStrLn "Even number"    
    else
        putStrLn "Odd number"   

In the code above, we define the variable v has a value of 10. Then check it v is odd (odd nunber) or even number.

Here we use keywords do. We will learn about this keyword later.

Also Haskell has 2 functions to check if a number is odd or even or not, that is odd and even. For example:

main :: IO ()
main = do
    let v = 9
    if v == 0 then
        putStrLn "Zero is neither odd nor even"
    else if even v then
        putStrLn "Even number"
    else if odd v then
        putStrLn "Odd number"
    else 
        putStrLn "Unknown"

Hope this helps!

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *