asdfghjkl
asdfghjkl

Reputation: 393

unzipping list into two separate lists in racket

I am trying to write a function called unzip that takes a list and evaluates to a list of two lists that have alternating elements of the original list.

so far this is what i have:

(define (unzip lst)
    (if (null? lst)
        '()
        (...

this is how it should work:

(unzip '(1 a 2 b 3 c)) should evaluate to ((1 2 3) (a b c))

Upvotes: 0

Views: 2005

Answers (1)

C. K. Young
C. K. Young

Reputation: 223003

What you have is on the wrong track, sorry. (Think about what the result should be if given an empty input.) Similar to my answer to your last question, here's a skeleton solution:

(define (unzip l)
  (if (null? l) ???
      (let ([next (unzip ???)])
        (list (cons ??? ???) ???))))

Fill in the ???s appropriately. (Yes, my solution is tested and works for both odd and even inputs.)

Upvotes: 1

Related Questions