diallo.ms
2019-5-8 00:39:58

Hello,

I am working on the following exercise:

Design a data representation for finite and infinite sets so that you can represent the sets of all odd numbers, all even numbers, all numbers divisible by 10, and so on.

Design the functions add-element, which adds an element to a set.; union, which combines the elements of two sets; and intersect, which collects all elements common to two sets.

Hint: Mathematicians deal with sets as functions that consume a potential element ed and produce #true only if ed belongs to the set.


Here is a sample of what I did (using the Intermediate Student with Lambda language <=> ISL+): ; A set is a function: ; [X] [X -&gt; Boolean] ; Interpretation: if s is a set and x an element, (s x) produces #t if x belongs to s, #f otherwise. ; Number -&gt; Boolean ; Set of even numbers. (check-expect (even-numbers 5) #f) (check-expect (even-numbers 8) #t) (define even-numbers (lambda (n) (even? n))) ; Number -&gt; Boolean ; Set of numbers divisible by 10. (check-expect (divisible-by-10 20) #t) (check-expect (divisible-by-10 3) #f) (define divisible-by-10 (lambda (n) (zero? (modulo n 10)))) ; [X] Set -&gt; [X -&gt; Boolean] ; Adds an element e to a set s. (check-expect [(add-element even-numbers) 5] #f) (define (add-element s) (lambda (e) (s e))) At this point, I am bothered: * A set is also a “list” in which there are no duplicates. By defining a set as a function, I don’t see where we make sure there are no duplicates. * We also can’t store elements by using functions as sets.

What am I not understanding ?

Thank you !


dan153
2019-5-8 01:54:53

If a function can only return true or false how could you know it had a duplicate element? (The-set 3) ; #t. How could you determine if the set contains 3 multiple times?


dan153
2019-5-8 01:55:39

And maybe rethink the assumption that a set is also a list. It seems you’ve painted yourself into a corner unnecessarily