Normal string comparisons only work with lexicographic ordering, not with string lengths.
So, you will need to write your own function to also check the length:
smaller :: String -> String -> Bool smaller s1 s2 | length s1 < length s2 = True | length s1 > length s2 = False | otherwise = s1 < s2
Or a little more general:
compareStrings :: String -> String -> Ordering compareStrings s1 s2 | length s1 < length s2 = LT | length s1 > length s2 = GT | otherwise = compare s1 s2
Example:
ghci> compare "ab" "z" LT ghci> compareStrings "ab" "z" GT
We got together with Monoids at the university last week, and we came up with this great alternative example of Ord :
instance Ord a => Ord [a] where compare = comparing length `mappend` comparing head `mappend` comparing tail
But if you do not quite understand this, I suggest you stick to the first definition :-)
Tom lokhorst
source share