Page 1 of 1

trees

Posted: Fri Nov 10, 2006 7:11 pm
by cormullion
I've been trying to grow trees (not really deliberately...) a bit like this:

Code: Select all

(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:

Code: Select all

(println (ref "0004" l))
;-> (2 3 1 4)
but I really want to produce is this:

Code: Select all

("0" "01" "0002" "00004" )
- kind of like a series of signposts... Any suggestions?

Posted: Fri Nov 10, 2006 7:44 pm
by Lutz
I don't see the logic of:

Code: Select all

("0" "01" "0002" "00004" )
Do you mean the node branch points leading to "0004" ? that would be:

Code: Select all

("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:

Code: Select all

> (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

Posted: Fri Nov 10, 2006 8:35 pm
by Sammo
Almost!

The 'chop' sequence needed is:

Code: Select all

(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:

Code: Select all

(map (fn (i) (l (append (chop v i) '(0)))) (append '(0) (sequence 2 (length v)))) --> ("0004" "002" "01" "0")

Posted: Fri Nov 10, 2006 9:07 pm
by cormullion
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.. ;-)

Posted: Sat Nov 11, 2006 12:21 am
by m i c h a e l
Hi cormullion,
cormullion wrote: 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 karass ;-)

m i c h a e l

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