<br><br><div class="gmail_quote">On Wed, Jun 20, 2012 at 12:35 PM, David Delfieu <span dir="ltr"><<a href="mailto:david.delfieu@univ-nantes.fr" target="_blank">david.delfieu@univ-nantes.fr</a>></span> wrote: <br>[...]<br>
<blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
(define lp (cons (first l) (Vn 0 l)))<br>
<br>
; lp is (e_1 (e_3 (e_5 (e_7 (e_9 e_11) (e_10)) (e_8 (e_10) (e_11))) (e_6 (e_8 (e_10) (e_11)) (e_9 e_11))) (e_4 (e_6 (e_8 (e_10) (e_11)) (e_9 e_11)) (e_7 (e_9 e_11) (e_10))))<br>
<br>
; I would like to unflat this tree and to have all the paths of the tree and to obtain :<br>
<br>
; ((e_1 e_3 e_5 e_7 e_9 e_11) (e_1 e_3 e_5 e_7 e_10) (e_1 e_3 e_5 e_8 e_10) (e_1 e_3 e_5 e_8 e_11) (e_1 e_3 e_6 e_8 e_10) (e_1 e_3 e_6 e_8 e_11) (e_1 e_3 e_6 e_9 e_11) (e_1 e_4 e_6 e_8 e_10)<br>
;(e_1 e_4 e_6 e_8 e_11) (e_1 e_4 e_6 e_9 e_11) (e_1 e_4 e_7 e_9 e_11) (e_1 e_4 e_7 e_10))<br>
<br>
; I am trying, without any success (since 3 weeks), to obtain this list of lists, perhaps because i need a local list under building which change when a leave is reached<br>
<br>
My poor and un-complete solution is<br>
(define (1? l)<br>
(equal? (length l) 1))<br>
<br>
(define (simple-list? l)<br>
(or (1? l)<br>
(for/and ( [e l]) (not (list? e)))))<br>
<br>
(define (flat r g d)<br>
(cond<br>
[(simple-list? g) (cons r g)]<br>
[(simple-list? d) (cons r d)]<br>
[else (list (cons r (flat (car g) (second g) (third g)))<br>
(cons r (flat (car d) (second d) (third d))))]))<br>
<br>
lp<br>
(flat (car lp) (second lp) (third lp))<br>
<br>
<br>
; which gives the un-complete solution ((e_1 (e_3 (e_5 e_7 e_9 e_11) (e_5 e_8 e_10)) (e_3 e_6 e_9 e_11)) (e_1 (e_4 e_6 e_9 e_11) (e_4 e_7 e_9 e_11)))<br>
<br>
I could easily solve this problem in imperative language (C).<br>
Do you think it could exist an elegant solution ?<br>
I am doubting of a good solution in fonctionnal language as Racket.<br></blockquote><div><br>Couldn't resist the troll... <br><br>You mean something like this?<br><br>#lang racket<br><br>; lp is <br>(define lp<br> '(e_1 (e_3 (e_5 (e_7 (e_9 e_11) (e_10))<br>
(e_8 (e_10) (e_11)))<br> (e_6 (e_8 (e_10) (e_11))<br> (e_9 e_11)))<br> (e_4 (e_6 (e_8 (e_10) (e_11))<br> (e_9 e_11)) <br> (e_7 (e_9 e_11)<br>
(e_10)))))<br><br>; I would like to unflat this tree and to have all the paths of the tree and to obtain :<br>(define lres<br>'((e_1 e_3 e_5 e_7 e_9 e_11)<br> (e_1 e_3 e_5 e_7 e_10)<br> (e_1 e_3 e_5 e_8 e_10)<br>
(e_1 e_3 e_5 e_8 e_11)<br> (e_1 e_3 e_6 e_8 e_10)<br> (e_1 e_3 e_6 e_8 e_11)<br> (e_1 e_3 e_6 e_9 e_11)<br> (e_1 e_4 e_6 e_8 e_10)<br> (e_1 e_4 e_6 e_8 e_11)<br> (e_1 e_4 e_6 e_9 e_11)<br> (e_1 e_4 e_7 e_9 e_11)<br> (e_1 e_4 e_7 e_10)))<br>
<br><br>(define (flat-list? l)<br> (and (list? l) (not (ormap list? l))))<br>#|<br>> (flat-list? '(a b e))<br>#t<br>> (flat-list? '(a b (e)))<br>#f<br>> (flat-list? '(a b (e) f))<br>#f<br>|#<br><br>
(define (comp tree)<br>
(cond [(empty? tree) '()]<br> [(flat-list? tree) (list tree)]<br> [(list? tree)<br> (let ([sublists (append-map comp (rest tree))])<br> (for/list ([sl sublists])<br> (cons (first tree) sl)))]<br>
))<br><br>(equal? lres (comp lp)) ; #t<br></div></div><br>(most probably not optimal, and only little tested; use with caution)<br>Laurent<br>