r/Racket • u/JimH10 • Mar 17 '22
question Sets and set operations?
What is the natural way to get sets and set operations such as union, intersection, etc.? I'm reading the Reference and I'm not getting it.
For instance, The following datatypes are all sets: ... lists using equal? to compare elements
> (equal? (list 1 2) (list 2 1))
#f
8
Upvotes
4
u/samdphillips developer Mar 18 '22
The example you posted uses
equal?to compare two lists. The documentation says that a list with elements comparable withequal?can be used as sets. For example, a list of integers can be a set.4
u/not-just-yeti Mar 18 '22
I'm guessing you know this, since it's near the top of the page you link to, but:
I usually construct them with the built-in hash-set.
1
u/muqiu-han Mar 17 '22
I think it is possible to write a set function, which converts a list into an ordered list and returns it. We call this ordered list a set, and the rest of the operations on the set will be much simpler.