The question is:
1. (a) The function ord :: Char -> Int converts a character to its corresponding
ASCII code. For example,
ord A = 65 ord B = 66 ... ord Z = 90
ord a = 97 ord b = 98 ... ord z = 122
Using ord, write a function f :: Char -> Int that converts a letter, lower case
or upper case, to its ordinal position in the alphabet. For example,
f A = 0 f B = 1 ... f Z = 25
f a = 0 f b = 1 ... f z = 25
For any character that is not a letter, f should return an error.
The solution is below and I stop understanding it right after ''a' <= x && x <= 'z' =' on the first line. Could somebody please walk me through exactly what is happening here? Thank you in advance.
-- 1a.
f :: Char -> Int
f x | 'a' <= x && x <= 'z' = ord x - ord 'a'
| 'A' <= x && x <= 'Z' = ord x - ord 'A'
x1a = f 'A' == 0 && f 'B' == 1 && f 'Z' == 25 &&
f 'a' == 0 && f 'b' == 1 && f 'z' == 25


LinkBack URL
About LinkBacks




Reply With Quote

I was just a little confused. Thanks for the help. One last question

Bookmarks
Algorithms and Data Structures
Java tutorials
Algorithms Forum