views:

102

answers:

2

Ok so I am trying to make a "make-set" program using objects. I have all ready programmed all the basic procedures for sets like union and intersect and member-of so this is what i ahve so far for my make-set program using object:

(define make-set
   (lambda ()
    (let ((s '()))
     (lambda (msg e)
       (case msg
        ('add (set! s (cons e s))
        ('member? (member-of e s))
        ('intersect (intersect e s))
        ('superset (superset e s))
        ('subset (subset e s)))))))

So does this work..is this using object? If not could you show me how...Thanks

+1  A: 

It might not be object-oriented in PLT Scheme way. According to the document, PLT Scheme has its own OO system.

So, OO-based set might be implemented as following:

;; define set class
(define set%
    (class object%
        (init init-elems)
        (define elements elems)
        (super-new)
        (define/public (add elem)
           ;; TODO: check if “elem” is already included in “elements”
           (set! elements (cons elem elements)))
        ;; and so on...
        ))
habe
+2  A: 

Yes, this can be seen as a use of objects. You're essentially "rolling your own" object system by modeling each object as a function that responds to messages.

Note, however, a major shortcoming of the system as you've written it. Suppose you want to intersect two sets, both represented as objects. Calling your "intersect" method won't work. Do you see why?

More generally, PLT Scheme (and many other languages) provide a huge amount of infrastructure to support the notion of objects; you might want to take a look at these. Let me know if you need pointers.

John Clements