I need help on two problems for my computer science class. The problems are below.
Write a function contains-all? that consumes two lists of numbers and returns true if the first list contains all elements of the second one and false otherwise. For simplicity assume that neither list contains the same element more than once.
Write a function common-elements that takes two lists of numbers and returns the list of all elements that appear in both lists.
For number 1 I have the following code so far:
(define (contains-all? lon lon2)
(cond
[(and (empty? lon) (empty? lon2)) true]
[(or (empty? lon) (empty? lon2)) false
[(equal? (first lon) (first lon2)) (contains-all? (rest lon) (rest lon2))]
[else false]))
My check-expects are the following:
(check-expect (contains-all? empty empty) true)
(check-expect (contains-all? empty (list 1 2 3)) false)
(check-expect (contains-all? (list 1 2 3) empty) false)
(check-expect (contains-all? (list 1 2 3) (list 3 2 1)) true)
(check-expect (contains-all? (list 1 2 4 6 8) (list 6 8 4)) true)
(check-expect (contains-all? (list 1 2 8 6) (list 1 2 6 4)) false)
I know some of the check-expects will fail and that is what I need help with.
For problem 2 I have this so far
(define (common-elements lon lon2)
(cond
[(and (empty? lon) (empty? lon2)) empty]
[(or (empty? lon) (empty? lon2)) empty]
[(equal? (first lon) (first lon2))
(cons (first lon) (common-elements (rest lon) (rest lon2)))]
[(not (equal? (first lon) (first lon2))) (common-elements (first lon) (first lon2))]
;[else (common-elements (first lon) (rest lon2))]))
Check expects are the following:
(check-expect (common-elements empty empty) empty)
(check-expect (common-elements empty (list 1 2)) empty)
(check-expect (common-elements (list 1 2) empty) empty)
(check-expect (common-elements (list 1 2 3) (list 1 2 4)) (list 1 2))
(check-expect (common-elements (list 3 2 1) (list 2 1)) (list 2 1))
I have the same problem with number 2 and need help on them.