Im sorry i have posted codes the entire day, now i have this one and i have no idea why its not working. I have an error that doesnt tells me exactly what my problem is, someone told me to turn on the warnings so it could be easier but i dont know how do to that. If you give this code a monomian, it should give you the monomian as a string. The monomian is a (a,b), so thats why the code is full of fst and snd.
showMon :: Monomio -> String
showMon = \m -> case (fst m) == 0 of{ True -> " ";
False -> case (fst m) == 1 of{ True -> case (snd m) == 0 of{ True -> "1";
False -> case (snd m) == 1 of{ True-> "x";
False -> "x^" ++ (show (snd m));}}
False -> case (fst m) < 0 of{ True -> case (snd m) == 0 of{ True -> show 1;
False -> case (snd m) == 1 of { True -> "-x";
False -> "-x^" ++ show (snd m);}}
False -> case snd m == 0 of{ True -> show 1;
False -> case snd m == 1 of{ True-> (show fst m) ++ "x";
False-> (show fst m) ++ "x^" ++ show (snd m);}}}}}
Polinomios.hs:146:108: error:
Unexpected case expression in function application:
case (snd m) == 0 of
True -> "1"
False
-> case (snd m) == 1 of
True -> "x"
False -> "x^" ++ (show (snd m))
You could write it with parentheses
Or perhaps you meant to enable BlockArguments?
|
146 | False -> case (fst m) == 1 of{ True -> case (snd m) == 0 of{ True -> "1"; |
Here's your code, re-indented for clarity. It is still quite hard to read.
showMon :: Monomio -> String
showMon = \m -> case (fst m) == 0 of
{ True -> " ";
False -> case (fst m) == 1 of
{ True -> case (snd m) == 0 of
{ True -> "1";
False -> case (snd m) == 1 of
{ True-> "x";
False -> "x^" ++ (show (snd m));
}
} -- **
False -> case (fst m) < 0 of
{ True -> case (snd m) == 0 of
{ True -> show 1;
False -> case (snd m) == 1 of
{ True -> "-x";
False -> "-x^" ++ show (snd m);
}
} -- **
False -> case snd m == 0 of
{ True -> show 1;
False -> case snd m == 1 of
{ True-> (show fst m) ++ "x";
False-> (show fst m) ++ "x^" ++ show (snd m);
}
}
}
}
}
In the points marked with -- **
above a semicolon is missing, making the compiler produce a weird error message, since we are essentially trying to apply a case
expression to some arguments in this way.
Some suggestions to improve this:
You don't have to use fst,snd
, you can pattern match on the input argument. Instead of \m -> ...
, you can use \(m1,m2) -> ...
and then use the two components directly.
You don't have to check fst m == 0
, fst m == 1
in that way. You can instead use (trying to mimic your style)
case m1 of -- recall m1 is (fst m)
{ 0 -> .... ; -- it is 0
1 -> .... ; -- it is 1
_ -> .... -- it is something else
}
In principle, with guards you can even incorporate checks like your fst m < 0
.