Re: making in a loop - or an sequentially building a nested list
Hi Joe, > Hi Alex, I revisited my code based on your reply. I like the con/cons > approach and developed a small helper class. I'm sharing here in case > others find it helpful: Nice! > (class +AppendList) > > (dm T () (=: L (cons))) > > (dm append> (Node Parent) > (let L (or Parent (: L)) > (ifn (car L) > (prog (set L Node) L) > (let (Len (length L) O (cons)) > (con (nth L Len) O) > (set (nth L (inc Len)) Node) >O ) ) ) ) > > (dm append-list> (Parent) > (car (append> This (cons) Parent))) > > (dm list> () (: L) ) I see. OOP (i.e. the dynamic message passing, and the space for the object itself) has some overhead, though. You could consider using 'fifo': (off L) (fifo 'L 'Mother 'Father) ... (fifo 'L ..) and in the end cut it off to get a normal list: (prog1 (cdr L) (con L)) Another possibility might be to use 'make' and 'link' etc., but use 'made' to dynamically switch between different make-environments. 'made' is in fact not very expensive. But 'fifo' is probably the most efficient way. ♪♫ - Alex -- UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
Re: making in a loop - or an sequentially building a nested list
Hi Joe, > (setq L NIL) > (list-make 'L) > (list-make 'L) > (list-link 'L 'parent) > (list-make 'L) > (list-link 'L 'child) > (list-link 'L 'child) > (list-close-all 'L) > > -> ((parent (child child))) > > I don't know how I feel about it... I'm not sure if I grasp the spirit of your intention, but how do you feel about this: (setq L (cons)) (set L (cons)) (set (nth L 1 1) 'parent) (con (nth L 1 1) (cons)) (set (nth L 1 2) (cons)) (set (nth L 1 2 1) 'child) (con (nth L 1 2 1) (cons)) (set (nth L 1 2 2) 'child) L -> ((parent (child child))) ♪♫ - Alex -- UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
Re: making in a loop - or an sequentially building a nested list
Here's a naive implementation: (de list-make ("Lst") (queue "Lst" "(")) (de list-link ("Lst" Node) (queue "Lst" Node) (queue "Lst" " ")) (de list-close ("Lst") (queue "Lst" ")")) (de list-close-all ("Lst") (let (Open (length (sect (val "Lst") (list "(" ))) Close (length (sect (val "Lst") (list ")" (for X (- Open Close) (queue "Lst" ")"))) (car (str (pack (val "Lst") (setq L NIL) (list-make 'L) (list-make 'L) (list-link 'L 'parent) (list-make 'L) (list-link 'L 'child) (list-link 'L 'child) (list-close-all 'L) -> ((parent (child child))) I don't know how I feel about it... On Mon, Nov 26, 2012 at 1:11 PM, Joe Bogner wrote: > I'm sure I'm making this harder than it is. I spent several hours > tinkering around trying to find a way to sequentially build a nested list. > > make and link have spoiled me. > > Instead of this: > > : (make (link (make (link 'parent (make (link 'child 'child)) > -> ((parent (child child))) > > or > > : (list (list 'parent (list 'child 'child > -> ((parent (child child))) > > I would like to do > > (setq L NIL) > (list-make 'L) > (list-make 'L) > (list-link 'L 'parent) > (list-make 'L) > (list-link 'L 'child) > (list-link 'L 'child) > > Or something along those lines. The reason for the question is that I find > myself building things in for loops and it seems like I would need to use > some form of recursion to build it with make or list. > > I've had some success with push and queue but was wondering if there's a > more elegant way. > >