Main Menu

trees

Started by cormullion, November 10, 2006, 11:11:46 AM

Previous topic - Next topic

cormullion

I've been trying to grow trees (not really deliberately...) a bit like this:


(set 'l '
  ("0"
    ("00"
      ("000"
        ("0000" "0001"))
      ("001"
        ("0000" "0001" "0002" "0003"))
      ("002"
        ("0000" "0001" "0002" "0003"))
      ("003"
        ("0000" "0001" "0002" "0003"))
      ("004"
        ("0000" "0001" "0002" "0003"))
      ("005"
        ("0000" "0001" "0002")))
    ("01"
      ("000"
        ("0000" "0001" "0002"))
      ("001"
        ("0000" "0001" "0002"))
      ("002"
        ("0000" "0001" "0002" "0003" "0004")))
    ("02"
      ("000"
        ("0000" "0001" "0002"))
      ("001"
        ("0000" "0001" "0002"))
      ("002"
        ("0000" "0001" "0002")))))


I think that's right. What I want to do is to locate an element and find its 'address'. I can do this:


(println (ref "0004" l))
;-> (2 3 1 4)


but I really want to produce is this:


("0" "01" "0002" "00004" )

- kind of like a series of signposts... Any suggestions?

Lutz

#1
I don't see the logic of:


("0" "01" "0002" "00004" )

Do you mean the node branch points leading to "0004" ? that would be:


("0" "01" "002" "0000")

and you can get those by successively chopping of the last member in the found index vector and replacing it with 0:


> (set 'v (ref "0004" l)) => (2 3 1 4)

(l (append (chop v 1) '(0))) => "0000"

(l (append (chop v 2) '(0))) => "002"

(l (append (chop v 3) '(0))) => "01"

(l (append (chop v 4) '(0))) => "0"

; or all in one shot

(map (fn (i) (l (append (chop v i) '(0)))) (sequence 1 (length v)))
=> ("0000" "002" "01" "0")


Lutz

Sammo

#2
Almost!



The 'chop' sequence needed is:
(l (append (chop v 0) '(0))) --> "0004"
(l (append (chop v 2) '(0))) --> "002"
(l (append (chop v 3) '(0))) --> "01"
(l (append (chop v 4) '(0))) --> "0"

The following seems to work:
(map (fn (i) (l (append (chop v i) '(0)))) (append '(0) (sequence 2 (length v)))) --> ("0004" "002" "01" "0")

cormullion

#3
Thanks! I knew you guys could see the wood through the trees!



I'm not sure exactly what I'm doing, but you're helping me do it, anyway.. ;-)

m i c h a e l

#4
Hi cormullion,


Quote from: "cormullion"
I'm not sure exactly what I'm doing, but you're helping me do it, anyway.. ;-)


I knew you and I were in the same http://en.wikipedia.org/wiki/Bokononism">karass ;-)



m i c h a e l



P.S. Congratulations on winning the t-shirt!