1
votes

I am supposed to create a function that takes two lists as the arguments and returns a list of pairs from combining the input lists where the pairs are a combination of the elements at the given index.

Example Input:

(zipper '(1 2 3) '(4 9 5 7))

Example Output:

'((1 4) (2 9) (3 5)) 

If one input list is longer than the other, extra elements of the longer list are ignored. The implementation has to be recursive.

The code I created:

(define (zipper list1 list2)
(if (or (empty? list1) (empty? list2))
  '()
(list (list (zipper (rest list1) (rest list2)) ))))

The output:

'((((((((()))))))))
'((((((()))))))
'()

Can someone please help? I've also tried using 'cons' to put the elements together, so am not sure how the elements are getting lost. I'm brand new to Racket. Thanks.

1
Where is this a b c coming from in the test case? Provide more test cases. I am sure I can do it but not with the given information (What is a single pair of lists? that is not what your output illustrates. You mean a list of pairs?) - Tyler Nichols
You are absolutely right. I had copied the wrong test case into my post. Edited. - Brian Jay

1 Answers

3
votes

Pretty simple, all we have to do is make a function that will return empty as a base case if either list is empty, otherwise we will cons the (first list1, first list2) to the recursive call.

(define (zipper list1 list2)
  (cond [(or (empty? list1)
             (empty? list2)) empty]
        [else (cons 
               (cons (first list1)
                     (first list2)) ;; List of first elements
               (zipper (rest list1) (rest list2)))]))