Union-set for sets represented as unordered lists

Implement the union-set operation for the unordered-list representation of sets.

    # g7845123
    1 year ago

    The order shouldn't matter when validating the solution. E.g. below code should be correct but doesn't pass the test

    (define (union-set s1 s2)
        (if (null? s2)
            (union-set (adjoin-set (car s2) s1) (cdr s2))

    The function adjoin-set is defined in book.

    # Anton Burenkov
    1 year ago

    Thanks. This solution passes all tests except the last one.

    (check-equal? (union-set '() second) second)

    which is an edge case - the first element of the list is empty and the second element is expected as a result.

    In this case, it is logical to return the second element unchanged, but there is clearly no such condition. This test has been replaced by a more general one.

Authentication required

You must log in to post a comment.