Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to convert a list to a set in Scheme?

Tags:

lisp

scheme

I want to test for equality amongst lists, but I really only care that the members are the same, not the ordering. Are there any simple operators to check for this?

A rather trivial example

(my-equal? (a b) (b a))

Should return #t.

Obviously, this particular example can easily be done by checking for both lists and then reversing the second and checking again

(or (equal? (a b) (b a)) (equal? (a b) (reverse (b a)))

But is there a way in general? I could have a stab at writing a function but I can only imagine something quite complicated that would do the work. I am guessing this must be a fairly common need and I wonder if scheme has an operator in-built that can do the work here.

I am using mit-scheme 9.0.1

like image 477
jones Avatar asked Oct 16 '25 11:10

jones


2 Answers

If your implementation has SRFI 1 available, there is lset= to achieve what you want:

Set operations on lists

like image 93
zw324 Avatar answered Oct 19 '25 09:10

zw324


If you were using Racket, I would point you to this page, with built-in support for sets.

No, that's not answering your question, I know.

like image 36
John Clements Avatar answered Oct 19 '25 09:10

John Clements



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!