2010年7月15日木曜日

Flatten a nested list structure

;; P07 (**) Flatten a nested list structure.
;; Transform a list, possibly holding lists as elements into a `flat' list by replacing each list with its elements (recursively).
;; Example:
;; * (my-flatten '(a (b (c d) e)))
;; (A B C D E)
;; Hint: Use the predefined functions list and append.
(define (my-flatten lst)
(if (null? lst)
'()
(let ((head (car lst)) (flat-tail (my-flatten (cdr lst))))
(append ((if (pair? head) my-flatten list) head) flat-tail))))
view raw p07.ss hosted with ❤ by GitHub

0 件のコメント:

コメントを投稿