Gal Yakir
Gal Yakir

Reputation: 155

Replace list into list of list

How can I build a function that receives a list and builds 2 lists that on the first one are all the even numbers and on the other one are all the odd numbers?

For the input: (fun '(1 2 3 4 5 6))
the output will be: ((2 4 6) (1 3 5)).

Upvotes: 2

Views: 50

Answers (1)

Óscar López
Óscar López

Reputation: 236142

There's a built-in for that, simply use partition and provide the right predicate. The rest of the code is just for capturing the returned values and building the output list:

(define (my-partition lst)
  (let-values ([(evens odds) (partition even? lst)])
    (list evens odds)))

For example:

(my-partition '(1 2 3 4 5 6))
=> '((2 4 6) (1 3 5))

Upvotes: 3

Related Questions