home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / xl21hos2.zip / ART.LSP < prev    next >
Lisp/Scheme  |  1995-12-27  |  2KB  |  64 lines

  1. ; This is an example using the object-oriented programming support in
  2. ; XLISP.  The example involves defining a class of objects representing
  3. ; dictionaries.  Each instance of this class will be a dictionary in
  4. ; which names and values can be stored.  There will also be a facility
  5. ; for finding the values associated with names after they have been
  6. ; stored.
  7.  
  8. ; Create the 'Dictionary' class and establish its instance variable list.
  9. ; The variable 'entries' will point to an association list representing the
  10. ; entries in the dictionary instance.
  11.  
  12. (setq Dictionary (send Class :new '(entries)))
  13.  
  14. ; Setup the method for the ':isnew' initialization message.
  15. ; This message will be send whenever a new instance of the 'Dictionary'
  16. ; class is created.  Its purpose is to allow the new instance to be
  17. ; initialized before any other messages are sent to it.  It sets the value
  18. ; of 'entries' to nil to indicate that the dictionary is empty.
  19.  
  20. (send Dictionary :answer :isnew '()
  21.         '((setq entries nil)
  22.           self))
  23.  
  24. ; Define the message ':add' to make a new entry in the dictionary.  This
  25. ; message takes two arguments.  The argument 'name' specifies the name
  26. ; of the new entry; the argument 'value' specifies the value to be
  27. ; associated with that name.
  28.  
  29. (send Dictionary :answer :add '(name value)
  30.         '((setq entries
  31.                 (cons (cons name value) entries))
  32.           value))
  33.  
  34. ; Create an instance of the 'Dictionary' class.  This instance is an empty
  35. ; dictionary to which words may be added.
  36.  
  37. (setq d (send Dictionary :new))
  38.  
  39. ; Add some entries to the new dictionary.
  40.  
  41. (send d :add 'mozart 'composer)
  42. (send d :add 'winston 'computer-scientist)
  43.  
  44. ; Define a message to find entries in a dictionary.  This message takes
  45. ; one argument 'name' which specifies the name of the entry for which to
  46. ; search.  It returns the value associated with the entry if one is
  47. ; present in the dictionary.  Otherwise, it returns nil.
  48.  
  49. (send Dictionary :answer :find '(name &aux entry)
  50.         '((cond ((setq entry (assoc name entries))
  51.           (cdr entry))
  52.          (t
  53.           nil))))
  54.  
  55. ; Try to find some entries in the dictionary we created.
  56.  
  57. (send d :find 'mozart)
  58. (send d :find 'winston)
  59. (send d :find 'bozo)
  60.  
  61. ; The names 'mozart' and 'winston' are found in the dictionary so their
  62. ; values 'composer' and 'computer-scientist' are returned.  The name 'bozo'
  63. ; is not found so nil is returned in this case.
  64.