function (infix) (x < Int y on Int) return in Bool
pre true.
post The returned value is true if x < y and is false otherwise.
Equality is also a comparison function. This is normally written using the infix symbol =. If x and y are numbers, then x = y is true if the numbers x and y are the same and is false if they are not. If one is working with real numbers on a computer, then one needs to be very careful with tests of equality. There will be an element of approximation in the way real numbers are stored, and this may result in a computer reporting real numbers as equal when they are only approximately equal. However, we shall confine our attention to integers, where there is no such problem in deciding whether two numbers are the same.
Sometimes we may wish to test to see whether two characters are the same. (This can be done by comparing their ASCII codes, for example.) A computer will need to work in a different way when comparing two integers for equality from that needed when comparing two characters for equality. These are different processes. Equality of numbers is a function with signature Int × Int → Bool; equality of characters, on the other hand, has signature Char × Char → Bool. Since these are different functions, we will give them different identifiers. We will write = Int for equality of integers and = Char for equality of characters.
function (infix) (x = Int y on Int) return in Bool
pre true.
post The returned value is true if the integers x and y are equal and is false otherwise.
function (infix) (x = Char y on Char) return in Bool
pre true.
post The returned value is true if the characters x and y are the same and is false otherwise.
One could give a binary operation < that compares characters by comparing their ASCII codes. Again, this is a different function from that of comparing integers (which is why we used a subscript Int in the identifier < Int for comparison of integers above).
Note that in mathematics the term binary operation would only be used for a total function whose return type is the same as the type of the inputs.
Activity 27
Give a full description of an infix function corresponding to < Char comparing two characters by comparing their ASCII codes.
Discussion
A description is given below.function (infix) (x < Char y on Char) return in Bool
pre true.
post The returned value is true if ASC(x) < Int ASC (y) and is false otherwise.
No comments:
Post a Comment