CSE 341 Spring 96 -- 4/10/96 ======================================================= -- Problem set out Friday 4/5, due Monday 4/15 ====================== Last time: 1. Equality checking EQ, EQL, =, EQUAL 2. LET and lexical variables For today (and beyond): 1. Quick review of LET 2. Operations on lists 3. Local and anonymous functions ====================================================== Notes on equality testing (one more time!) Here are the technical details. 1. EQ EQ returns T for two objects if and only if they are "physically" the same. That is, (EQ X Y) is true only if X and Y point to the same place in memory. Any implementation of Common Lisp is _required_ to implement symbols such that two symbols with the same name occupy the same memory, thus (EQ 'A 'A) => T in any implementation of Lisp. Whether or not any _other_ data types compare EQ is implementation dependent. Generally it is the case that characters and fixnums will compare EQ, but you shouldn't count on it. 2. EQL Two objects are EQL if and only if 1. they are EQ, or 2. they are numbers with the same type or value, or 3. they are characters that represent the same character value. So EQL is strictly more general than EQ, and furthermore it is the default equality tester for many built-in functions like MEMBER or FIND. But you should also note that EQL is a little screwy in that it sort of but doesn't really do numeric equality correctly. 3. EQUAL Two objects are EQUAL if and only if 1. they are EQL or 2. they are _sequences_ (that is, lists, strings, or vectors) that have the same number of elements and those elements are element-by-element EQUAL. So EQUAL is the only reasonable choice for comparing possibly nested lists, since it is a recursive definition that fits the recursive nature of nested lists. Now there is some stylistic debate about which among the equality testers to choose, since in many cases more than one will do the job. One school of thought say use the most specific equality tester that will work. So if you know the objects compared are symbols, use EQ, if you know they are numbers, use =, and so on. Another school of thought says use the most general tester available, the reasoning being you can always use the same one and don't have to think about it. In that case, EQUAL is the most reasonable choice. I personally feel the first way is better, just for documentation purposes: you send your reader a subtle message when you use EQ, for example, that you expect to be comparing symbols. But that being said, use whatever you're most comfortable with. We will certainly not take off any points for using a correct but overly general equality tester!