JR3652
JR3652

Reputation: 445

Scheme -Trouble returning a list of lists

This function is supposed to take 3 lists, and return a list of lists where each outputted sub list contains the 3 beginning elements of the input lists.

So the function would return a list ((1 2 10) (3 4 20) (9 8 30)).

I can't get the formatting to come out the way I want to. Any ideas on how I could get this to work out?

enter code here

Upvotes: 0

Views: 240

Answers (1)

soegaard
soegaard

Reputation: 31147

Let's workout a few examples:

; (zip3 '()  '()  '())          == ()                   
; (zip3 '(9) '(8) '(30))        == ((9 8 30))
; (zip3 '(3 9) '(4 8) '(20 30)) == ((3 4 20) (9 8 30)) 
                                == (cons '(3 4 20) (zip3 '(9) '(8) '(30))

This inspires the following:

(define (zip3 xs ys zs)
  (cond
    [(null? xs)  '()]
    [else        (let ([x   (car xs)] [y   (car ys)] [z   (car zs)]
                       [xs* (cdr xs)] [ys* (cdr ys)] [zs* (cdr zs)])
                   (cons (list x y z) (zip3 xs* ys* zs*)))]))

(zip3 '(1 3 9) '(2 4 8) '(10 20 30))

Upvotes: 1

Related Questions