// file: mail6.rex // author: keller // purpose: development of a mailing list processor, step 6 // The problem is now further extended as follows: Lists in the database // can include other lists or individuals. If a word in 'lists' is not // the name of a list in db, then the word itself is included in the result. // // select(items, db) creates a list of individuals in mailing lists named // in items, with no duplicates. If an item is not the name of a mailing // list, it is assumed to be an individual. select(item, db) = remove_duplicates(select1(item, db)); // select1 dispatches based on item: // If item is literally a list, it appends the results of // applying select1 to each item in the list // If item is the *name* of a mailing list, it appends the results of // applying select1 to each element of the list // Otherwise, item is just treated as an individual and a list of that // individual is returned. select1(item, db) = is_list(item) ? // item is: mappend((X) => select1(X, db), item) // a list : member(item, map(first, db)) ? mappend((X) => select1(X, db), rest(assoc(item, db))) // a list name : [item]; // an individual // built-in functions used: is_list, map, mappend, member, rest test() = select(["students", "joe", "math-majors", "fred"], db); test2() = select(["cs-faculty", "curriculum-comm"], db); // db is the database of mailing lists; It is a list of lists. // Each list consists of the name of the mailing list, followed by // the individuals on the list. db = [ ["students", "class of 99", "class of 00", "charlie" ], ["cs-majors", "mary", "john", "tom", "avi" ], ["math-majors", "alice", "tom", "franz", "yukio" ], ["class of 99", "mary", "tom", "yukio" ], ["class of 00", "john", "avi", "alice", "franz" ], ["cs-faculty", "george", "gwen", "prithi" ], ["math-faculty", "al", "gwen" ], ["curriculum-comm", "al", "prithi" ] ];