So I am trying to write a bit of Haskell and I've come to this problem that makes me wanna smash my head against a wall.
printGrade points = case points of
points | 0 <= points && points < 50 -> 5.0
points | 50 <= points && points < 54 -> 4.0
points | 54 <= points && points < 58 -> 3.7
points | 58 <= points && points < 62 -> 3.3
points | 62 <= points && points < 66 -> 3.0
points | 66 <= points && points < 70 -> 2.7
points | 70 <= points && points < 74 -> 2.3
points | 74 <= points && points < 78 -> 2.0
points | 78 <= points && points < 82 -> 1.7
points | 82 <= points && points < 86 -> 1.3
points | 86 <= points && points < 100 -> 1.0
note a b c d =
if d > 100 || c > 20
then return "Wrong input"
else if a == False || b == False
then printGrade d
else printGrade (c + d)
When I try to run the code it compiles without problems but actually calling the function brings this error
<interactive>:91:1: error:
• Ambiguous type variable ‘m0’ arising from a use of ‘print’
prevents the constraint ‘(Show (m0 [Char]))’ from being solved.
Probable fix: use a type annotation to specify what ‘m0’ should be.
These potential instances exist:
instance Show a => Show (Maybe a) -- Defined in ‘GHC.Show’
instance (Show a, Show b) => Show (a, b) -- Defined in ‘GHC.Show’
instance (Show a, Show b, Show c) => Show (a, b, c)
-- Defined in ‘GHC.Show’
...plus 13 others
...plus two instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In a stmt of an interactive GHCi command: print it
I know that it has something to do with then return "Wrong Input" but I don't know a way to fix this since I have to print out a string at some point. (I've tried show/print/putStrLn which leads to another error)
case
here, but only use guards? – Willem Van Onsemreturn
is not a keyword in Haskell: it is a function that is used for monads. Therefore I advice you not to use it, unless you know what you are doing. – Willem Van OnsemDouble
, you can not return a string (and you surely can not return a string inside a monad likereturn "abc"
). If you want to error out you can useif ... then 2.71 else error "message"
, but understand that this error will crash the program when evaluated, and the the caller won't be able to catch the error and handle it. Sometimes this is enough. The expressionerror ".."
fits any type. – chi