Reputation: 13
compLetters :: Char -> Char -> Char
compLetters a b = chr (min (ord a ord b))
I am trying to compare to characters to see which one appears first in the alphabet. I tried to do this using the above code but an error appears. Any help appreciated, thanks.
Upvotes: 0
Views: 7924
Reputation: 12070
Char is an instance of Ord, so you can just use (<).
> 'a' < 'b'
True
Upvotes: 0
Reputation: 54058
Since Char
implements Ord
, you can just use min
directly:
firstChar :: Char -> Char -> Char
firstChar a b = min a b
Or more simply
firstChar = min
Or you could just use min
in your code
The reason why your code was failing is syntax. You have
min (ord a ord b)
Which parses as
min (((ord a) ord) b)
Which says that ord
takes 3 arguments and min
takes 1 argument, but this doesn't type check. Instead you should have
min (ord a) (ord b)
Upvotes: 9