Ian Murphy
Ian Murphy

Reputation: 27

List of Lists in Scheme

I was wondering how I can combine certain lists together in Scheme.

In Particular, I want to combine together a list, such as:

 (list 20 (list 10 (list 5 0)))

into something like:

 (list 20 10 5 0)

Something simple would be appreciated. Thanks.

Upvotes: 1

Views: 305

Answers (2)

Óscar López
Óscar López

Reputation: 235984

In Racket, there's a built-in function that does exactly what you need, it's called flatten:

(define lst (list 20 (list 10 (list 5 0))))
(flatten lst)
=> '(20 10 5 0)

Upvotes: 1

Chiron
Chiron

Reputation: 20245

What have you tried so far? Have you considered writing a function that makes use of cons? or something like a flatten function?

Upvotes: 0

Related Questions