0

I need help on two problems for my computer science class. The problems are below.

  1. 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.

  2. 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.

  • Why `(check-expect (contains-all? (list 1 2 3) empty) false)`? It should return `true` according to the specification, since the first list contains all the elements of the second (given that the second is `empty`). And, since you are using DrRacket, why paste the program in a BASIC-like format? Please use proper indentation, or simple paste the code from DrRacket editor. – Renzo Dec 09 '15 at 05:58
  • I assume the lists are sorted (otherwise you have to sort them first). If (first lon) < (first lon2) then you need only skip the first element of lon, *not* of lon2. Skip both only if (first lon) = (first lon2). – uselpa Dec 09 '15 at 12:06

1 Answers1

0

As @uselpa points out, a lot depends on whether the numbers in the lists are guaranteed to be sorted. Your problem statement suggests no, but your code suggests yes. Do you have this guarantee? I'm guessing the answer is no.

Next: you suggest that this is a problem that requires generative recursion, but this doesn't appear to be the case. Specifically, both of these functions look like a straightforward instance of HtDP's Processing Two Lists Simultaneously: Case 1.

If your instructor is claiming that these require generative recursion, I claim that your instructor is mistaken :).

However, I can say that both of these problems will be substantially easier if you use the "wish list" approach to describe needed functions as you're building the main function.

Also, as @Renzo points out, you appear to have a bug in at least one of your test cases; the list containing 1,2, and 3 certainly contains all of the elements in the empty list.

John Clements
  • 16,895
  • 3
  • 37
  • 52