I have a funciton which compiles successfully in haskell.
It looks like this:
suffix :: Int -> String
suffix i
| i==0 = "th"
| i==1 = "st"
| i==2 = "nd"
| i==3 = "rd"
| i>=4 || i<=9 = "th"
| i<0 = error "Must be positive integer" ----------NOT WORKING?
| otherwise = suffix(i `rem` 10)
It does not work that I can tell at least with the error line for the i<0 guard. When I enter into GHCI and add a “-” to the a number: suffix -5 I get the same error message I would get if I completely left this case off.
I’d like it to display my error message obviously. Hopefully someone can spot my issue. Thanks!
Basic Haskell syntax weirdness. This is the only case like it in the language.
suffix -5issuffix - 5is(-) suffix 5. The-character is treated as an infix binary operator, like all punctuation-named functions in haskell are. That is, unless there are no characters in the expression before the-. The simplest way to do what you want is withsuffix (-5). By enclosing it in parens, you are making it syntactically a standalone expression, which means no characters precede the-in the expression.This creates its own weirdness, though, in that you can’t create an operator section for subtracting a value from the input.
(-5)is not the section that means\x -> x - 5, unlike any other other binary operator in the language. It’s kind of an unfortunate situation that unary negation and binary subtraction use the same symbol, because it means there have to be corner cases like this in Haskell’s syntax.