Saturday 30 April 2016

Haskell: if-else if-else construct


By using if-else if-else construct, you can choose number of alternatives.

An if statement can be followed by an optional else if...else statement, which is very useful to test various conditions using single if...else if statement.

if_else.hs
getGrade marks = 
    if ((marks < 0) || (marks > 100) )
        then
            "Invalid input marks should in between 0 and 100"
    else if (marks < 35)
        then 
            "You are failed"
    else if (marks < 50)
        then
            "You are passed and got third class"
    else if (marks < 60)
        then
            "You are passed and got second class"
    else if (marks < 70)
        then
            "You are passed and got first class"
    else
        "You are passed and got distinction"

*Main> :load if_else.hs
[1 of 1] Compiling Main             ( if_else.hs, interpreted )
Ok, modules loaded: Main.
*Main> 
*Main> getGrade 100
"You are passed and got distinction"
*Main> 
*Main> getGrade 101
"Invalid input marks should in between 0 and 100"
*Main> 
*Main> getGrade 35
"You are passed and got third class"
*Main> 
*Main> getGrade 45
"You are passed and got third class"
*Main> 
*Main> getGrade 55
"You are passed and got second class"
*Main> 
*Main> getGrade 65
"You are passed and got first class"
*Main> 
*Main> getGrade 75
"You are passed and got distinction"


Whenever you are passing negative numbers to a function, be careful, you may get following kind of error, it is because system will think that you are tying to subtract 10 from getGrade.
*Main> getGrade -10

<interactive>:227:1:
    Non type-variable argument in the constraint: Num (a -> [Char])
    (Use FlexibleContexts to permit this)
    When checking that it has the inferred type
      it :: forall a. (Num a, Num (a -> [Char]), Ord a) => a -> [Char]


How to resolve above error?
Specify the negative number in parenthesis to resolve above problem.
*Main> getGrade (-10)
"Invalid input marks should in between 0 and 100"
*Main> getGrade (-100)
"Invalid input marks should in between 0 and 100"


Lets rewrite the getGrades function using guards.

guards.hs
getGrade marks
    | ((marks < 0) || (marks > 100) ) = "Invalid input marks should in between 0 and 100"
    | (marks < 35) = "You are failed"
    | (marks < 50) = "You are passed and got third class"
    | (marks < 60) = "You are passed and got second class"
    | (marks < 70) = "You are passed and got first class"
    | otherwise = "You are passed and got distinction"

*Main> :load guards.hs
[1 of 1] Compiling Main             ( guards.hs, interpreted )
Ok, modules loaded: Main.
*Main> 
*Main> getGrade (-10)
"Invalid input marks should in between 0 and 100"
*Main> getGrade 10
"You are failed"
*Main> getGrade 20
"You are failed"
*Main> getGrade 30
"You are failed"
*Main> getGrade 40
"You are passed and got third class"
*Main> getGrade 50
"You are passed and got second class"
*Main> getGrade 60
"You are passed and got first class"
*Main> getGrade 70
"You are passed and got distinction"
*Main> getGrade 80
"You are passed and got distinction"
*Main> getGrade 102
"Invalid input marks should in between 0 and 100"

Previous                                                 Next                                                 Home

No comments:

Post a Comment