home *** CD-ROM | disk | FTP | other *** search
- ; =================================================================
- ; File OBJECT.LSP -- object-oriented programming example for XLisp
- ; (Corrected version for XLisp 1.7 by Brad Christie @ 76167,1461)
- ; =================================================================
-
- ; This is an example of using the object-oriented programming support in
- ; XLisp. The example involves defining a class of objects representing
- ; dictionaries. Each instance of this class will be a dictionary in
- ; which names and values can be stored. There is also a facility for
- ; finding the values associated with names after they have been stored.
-
-
- ; Create the 'Dictionary' class and establish its instance variables See
- ; page 14 of the XLisp 1.7 manual. The variable 'entries' will point to
- ; an association list representing the entries in the dictionary instance.
- ; Note that the proper format for the :new message here is as follows:
- ; (Class :new <ivars> <cvars> [<super-class>])
- ; <ivars> can be nil, but <cvars> MUST be non-nil!
-
- (setq Dictionary (Class :new '(entries) '(dummy)))
-
-
- ; Setup the method for the 'isnew' initialization message.
- ; This message will be send whenever a new instance of the 'Dictionary'
- ; class is created. Its purpose is to allow the new instance to be
- ; initialized before any other messages are sent to it. It sets the value
- ; of 'entries' to nil to indicate that the dictionary is empty.
-
- (Dictionary :answer :isnew '()
- '((setq entries nil) self))
-
-
- ; Define the message 'add' to make a new entry in the dictionary. This
- ; message takes two arguments. The argument 'name' specifies the name
- ; of the new entry; the argument 'value' specifies the value to be
- ; associated with that name.
-
- (Dictionary :answer :add '(name value)
- '((setq entries (cons (cons name value) entries)) value))
-
-
- ; Create an instance of the 'Dictionary' class. This instance is an empty
- ; dictionary to which words may be added.
-
- (setq d (Dictionary :new))
-
-
- ; Add some entries to the new dictionary.
-
- (d :add 'mozart 'composer)
- (d :add 'winston 'computer-scientist)
-
-
- ; Define a message to find entries in a dictionary. This message takes
- ; one argument 'name' which specifies the name of the entry for which to
- ; search. It returns the value associated with the entry if one is
- ; present in the dictionary. Otherwise, it returns nil.
-
- (Dictionary :answer :find '(name &aux entry)
- '((cond ((setq entry (assoc name entries))
- (cdr entry))
- (t nil))))
-
-
- ; Try to find some entries in the dictionary we created:
-
- (d :find 'mozart)
- (d :find 'winston)
- (d :find 'bozo)
-
-
- ; The names 'mozart' and 'winston' are found in the dictionary so their
- ; values 'composer' and 'computer-scientist' are returned. The name 'bozo'
- ; is not found so nil is returned in this case.
-
-