简体   繁体   中英

Racket List elements within a list

I have something that returns a list of elements, but sometimes the list of elements are also lists of elements.

Example of this would be:

(1 2 3 (4 5) (6 7 (8))) 

I can't seem to write a function that just converts it into list with just the elements.

(1 2 3 4 5 6 7 8)

Here's the answer that the original poster came up with, for the benefit of anyone happening upon this question:

(define (test expresssion)
   (cond ((empty? expresssion) null)
         ((not (list? expresssion)) (list expresssion))
         (else (append (test (first expresssion))
                       (test (rest expresssion))))))

(test '(A (B (C) D) A)) ; => '(A B C D A)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM