next up previous contents
Next: Bitwise (Logical) integer Up: CHAPTER.5: NUMERICAL AND Previous: Exercises

Testing for equality and inequality

It is often necessary to use conditional imperatives, in order to write flexible programs which do not always do the same thing. This requires the ability to test certain conditions, to see if they are true or false. An important class of such tests is testing for equality or similarity.

    ==  test any two objects. TRUE if they are identical:
        i.e. not really two objects but one and the same.
        (Strict equality). In Lisp this is referred to as EQ.

    =   test any two objects. TRUE if they are identical,
        OR if they are of the same type with the same elements,
        i.e. if they are 'similar'. In Lisp this is referred to
        as EQUAL
E.g.

    3 == 3      is TRUE because it's the same thing, the number 3
                that's referred to on both sides.
    3 == 5 - 2  is also TRUE for the same reason.

    [A B C] == [A B C]  is FALSE, since they are two lists
                Each time Pop-11 reads in '[ .... ]' it creates a
                new list, even if there was a similar one earlier.

    [A B C] = [A B C]  is TRUE, since they are two SIMILAR lists.

    'A STRING' == 'A STRING'    is FALSE, because there are two strings
but
    'A STRING' = 'A STRING'     is TRUE, because the two strings are
                                similar
Thus it is possible to have two lists or strings with the same components, but which are not the very same object, e.g. if you type in the string `silly' twice.

    'silly' == 'silly' =>
    ** <false>
If you type in the same number twice Pop-11 will not treat the two expressions as denoting two different objects. So 999 will always refer to the same number. Strings and lists are different.

Words have to be treated like numbers. Pop-11 has to know about certain words, e.g. "define", "if", "(" and to be able to recognize new occurrences of the very same word. So words are entered in a dictionary when they are first read in, and if an expression denoting a word with the same characters is read in later, then instead of creating a new object Pop-11 finds the same word in its dictionary and re-uses that.

Thus, words, unlike strings, are `standardised' in a dictionary, so that you cannot have two different words with the same characters. If you attempt to type in a second one, Pop-11 will find the original in its dictionary, and assume you wanted to refer to it. So both the following are TRUE:

    "CAT" == "CAT" and  "CAT" = "CAT"
Writing two expressions on either side of an equality operation produces a new expression, which denotes a truth-value, TRUE or FALSE. In other words = and == each take two arguments and produce one BOOLEAN result.



Aaron Sloman
Fri Jan 2 03:17:44 GMT 1998