trees

Notices and updates
Locked
cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

trees

Post 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?

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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

Sammo
Posts: 180
Joined: Sat Dec 06, 2003 6:11 pm
Location: Loveland, Colorado USA

Post 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")

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post 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.. ;-)

m i c h a e l
Posts: 394
Joined: Wed Apr 26, 2006 3:37 am
Location: Oregon, USA
Contact:

Post 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!

Locked