1

I am trying to create a function that takes two set objects and returns a new set object that is the intersection of those two objects while using an iterator.

Here are some functions that I used, basic-set1 and basic-set2 are set objects that are initially empty.

((basic-set1 'get-set))
>(d c b a)
((basic-set2 'get-set))
>(a b)
(define my-iterator3 basic-set1)
((my-iterator3 'next))
> d
((my-iterator3 'next))
> c
((my-iterator3 'hasnext))
> #t

My desired output

(intersection-sets basic-set1 basic-set2)
> (b a)

This is the code I have so far.

 (define (intersect-sets set1 set2)
  (define my-iterator3 ((set1 'get-iterator )))
  (define result (basic-set))
  (define (iter)
   (let ((x ((my-iterator3 'next))))
    (cond ((not ((my-iterator3 'hasnext))) result)
          (((set2 'element?) x)
          (begin ((result 'insert) x)

                 (iter)))
          (else
           (iter)))))             
  (iter))

Tested output:

(intersect-sets basic-set1 basic-set2)
>#<procedure:...Problem3.rkt:60:2

I'm kind of stumped. Any help would be appreciated.

spidey2623
  • 25
  • 3
  • Why do you feel the need to use an iterator? This can be done without one. Check out @Sylwester 's solutions on [this question](http://stackoverflow.com/questions/18129617/how-to-take-intersection-of-pairs-from-two-lists-in-scheme) – masukomi Nov 28 '15 at 00:02
  • I don't know of any Schemes that have a precise definition for the term "iterator". Could you elaborate on what an iterator is in this context and why you want to use it? – Alexis King Nov 28 '15 at 00:48
  • An iterator is an object that is associated with a collection of data. It provides two functions, `has-next?` which returns true or false depending on whether there is more data to access, and `next` which returns some element of the collection – spidey2623 Nov 28 '15 at 01:26
  • i have `has-next?` as `hasnext` in my code, i will change that later – spidey2623 Nov 28 '15 at 01:27
  • Can we assume the sets are sorted in the same order? Iterator sounds kind of like a queue. – WorBlux Nov 28 '15 at 01:48
  • I don't think we can make that assumption no. – spidey2623 Nov 28 '15 at 01:55
  • I don't know if we can, but on further reading of the code it shouldn't matter if the dispatch procedure returned by `(basic-set)` properly handles the message `'element?` – WorBlux Nov 28 '15 at 01:57
  • FYI, `cond` clauses can have more than two parts, and have and implicit `begin` wrapped around all parts of the clause after the first. – WorBlux Nov 28 '15 at 02:50

1 Answers1

1

As far as I can tell your code is correct. The first cond clause returns result, which is a procedure. If you want the set returned as a list try ((not ((my-iterator3 'hasnext))) ((result 'get-set))) as your first cond clause in iter

WorBlux
  • 1,423
  • 11
  • 20
  • is `'get-result'` the same as my `get-set`? – spidey2623 Nov 28 '15 at 02:05
  • hmm I still get the same procedure output. Should I do `get-set` when I call iter at the end of the main function? – spidey2623 Nov 28 '15 at 02:16
  • You could do one or the other but not both. Putting in the cond is a clearer IMHO. Try sticking another set of parenthsis around it. `((result 'get-set))` I think you implemented it differently then I would have. Your dispatch returns a thunk that returns the value, right? I would have just returned the value directly from the dispatch. – WorBlux Nov 28 '15 at 02:45
  • Ok so I got it to output a list with you what you said above, but the result is `(b)` rather than `(b a)` is there a reason for this? – spidey2623 Nov 28 '15 at 02:57
  • Hard telling not knowing, and without your (basic-set) procedure code. My guess is that 'element? is accidentally modifying the set, or 'insert is not implemented properly. Alternatively the bug may be in 'has-next improperly saying a set of 1 has no next. Just double check everything. Writing tests may help. – WorBlux Nov 28 '15 at 03:04
  • I think I found what was wrong with it, I didn't check whether the last element in the first set is in the second, because the iterator will not have a next after assigning x to the last value. – spidey2623 Nov 28 '15 at 04:13