[ create a new paste ] login | about

Link: http://codepad.org/XHCIeyzP    [ raw code | output | fork ]

zelah - Scheme, pasted on Jul 12:
1
2
3
4
5
6
7
8
9
10
11
12
(define (flatten tree)
  (cond ((null? tree) '())
        ((not (pair? tree)) tree)
        ((null? (car tree))
         (flatten (cdr tree)))
        ((pair? (car tree))
         (append (flatten (car tree))
                 (flatten (cdr tree))))
        (else (cons (car tree)
                    (flatten (cdr tree))))))

(display (flatten '((((()) 1 2) 3) (()) 4 (5))))


Output:
1
(1 2 3 4 5)


Create a new paste based on this one


Comments: