I am new to Haskell programming, I have a requirement to print the grades based on the marks using Guard. Please find below code.
Any help would be appreciate as I am getting compiler error.
Couldn't match expected type ‘Integer -> String’
with actual type ‘[Char]’
• In the expression: "MediumPass"
In an equation for ‘printMarks’:
printMarks mark
| mark >= 50 && mark <= 59 = "MediumPass"
| mark >= 60 && mark <= 69 = "High Pass"
| mark >= 80 && mark <= 100 = "Distinction"
| otherwise = error "invalid mark"
The code:
printMarks :: Integer -> Integer -> String
printMarks mark | mark >= 50 && mark <= 59 = "MediumPass"
| mark >= 60 && mark <= 69 = "High Pass"
| mark >= 80 && mark <= 100 = "Distinction"
| otherwise = error "invalid mark"
main = do
putStrLn "Printing Grade"
print(printMarks 51)
CodePudding user response:
The type of printMarks
should be Integer -> String
and not Integer -> Integer -> String
because printMarks
takes an Integer
and produces a String
.
printMarks :: Integer -> String
printMarks mark
| mark >= 50, mark <= 59 = "MediumPass"
| mark >= 60, mark <= 69 = "High Pass"
| mark >= 80, mark <= 100 = "Distinction"
| otherwise = error "invalid mark"
main = do
putStrLn "Printing Grade"
print(printMarks 51)
output
tarptaeya@Anmols-MBP Temp % runghc foo.hs
Printing Grade
"MediumPass"
In Haskell f :: a -> b -> c
means that if you call f
with an object of type a
, then it will return an function of type b -> c
.