list - How to write a “set-equal?” predicate? -


how build same? predicate returns #t (same? '(4 6) '(6 4))? i'm stuck writing (same? b)-predicate returns #t if a , b equal sets , #f otherwise. similar predicate (element? el set) determines if el element of set.

(and yes, homework i'm not after finished solution. need bump or few, in right direction since there's no found our teachers.)

we representing sets using lists. asked build need our selves. higher order functions map etc pretty banned.

the problem is element? , same? doesn't work with:

(same? '(4 6) '(6 4))<br/> (element? '(2 3) '(1 8 5 '(3 2) 4)) 

these should return #t, don't , understand why still can't fix it.

my element? looks this, , kind of knew work lists of same order question how can improve it? (setempty, setfirst, setrest defined null?, car , cdr. we've been asked make our own reason.)

(define element?   (lambda (x set)      (cond ((setempty? set) #f)       ((equal? x (setfirst set)) #t)       (else (element? x (setrest set)))       )    ) ) 

i have working set?-predicate looks this, might of use:

(define set?   (lambda (set)     (cond ((setempty? set) #t)           ((list? (setfirst set))             (if (element? (setfirst set) (setrest set))              #f              (set? (setfirst set))))           (else (if (element? (setfirst set) (setrest set))                 #f                 (set? (setrest set))                 )             )         )      )  ) 

this returns #t if list , "sublists" without duplicates. have procedure makes true set out of list duplicates works fine.

a few pointers, in right direction.

the element? procedure right, except shouldn't use equal? key comparison - should use same?, , same? should differentiate between 2 cases: whether elements compared atoms, or sets.

so it's not hard imagine correctness of whole exercise depends on implementation of same?. , in turn, depends on chosen set representation. there's whole chapter on representing sets in wonderful book sicp (including representing sets lists), should start reading it, bearings.

once have implemented primitive procedures sets, it's easy check if 2 sets equal, i'll leave implement setsize , setunion:

(= (setsize a) (setsize b) (setsize (setunion b))) 

or alternatively, pointed @sds in answer, can determine if 2 sets same if they're subsets of each other - , should implement subset? procedure on own:

(and (subset? b) (subset? b a)) 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -