I made a function to compare strings (of course this is an exercise I'm doing in order to learn and I'm well aware that the <, > operators both work with strings in most modern languages). In order to do some recursion I'm using pattern matching for functions, but I'm not really sure on what's going on. Here's my code:
compareStrings :: String -> String -> Char
compareStrings (x:xs) (y:ys)
| x > y = '>'
| x < y = '<'
| x == y = compareStrings xs ys
compareStrings [a] [b]
| a < b = '<'
| a > b = '>'
| a == b = '='
So, there are a lot of cases I'm not covering in my code, like one empty list and a singleton list, and an empty list and a normal list (multiple elements). And of course its symmetric counterparts. How can I make sure I check them all? Is there something going under the hood or is it just comparing strings (not chars, which was my intention) at some point and I'm not aware of it?
- To sum up, my question is: Am I covering all the cases that could arise with my code, and if it's not the case, how could I make sure? And how would I deal with symmetric cases (like first list empty but the second one not, and the other way round) without declaring two different patterns.