user2849651
user2849651

Reputation: 13

Haskell comparing two chars

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

Answers (2)

jamshidh
jamshidh

Reputation: 12070

Char is an instance of Ord, so you can just use (<).

> 'a' < 'b'
  True

Upvotes: 0

bheklilr
bheklilr

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

Related Questions