newLISP Fan Club

Forum => newLISP in the real world => Topic started by: cormullion on May 03, 2010, 10:25:33 AM

Title: Selecting part of nested list?
Post by: cormullion on May 03, 2010, 10:25:33 AM
If i have a nested list, is it easy or possible to select a slice of it?



For example, say I find the slice I want using ref and store the start and end points in symbols:


(println start-pos)
;-> ((0 3 1072))
(println end-pos)
;-> ((0 3 4973))


How can I remove everything outside that range? Or is it impossible?
Title: Re: Selecting part of nested list?
Post by: cormullion on May 06, 2010, 11:34:14 AM
I resorted to doing it the old-fashioned way... :)


(set 'start-pos (ref  '(h2 "4. Functions in alphabetical order") xml-source))
(set 'end-pos (ref  '(a ((name "appendix") (id "appendix")))  xml-source ))

(for (x (last start-pos) (last end-pos))
   (push (xml-source (extend (chop start-pos) (list x))) res -1))
Title: Re: Selecting part of nested list?
Post by: Lutz on May 06, 2010, 04:58:14 PM
Ahh, now I understand, what you were looking for:



start-pos   ;=>  (0 3 1072)
end-pos   ;=> (0 3 4973)

(set 'start (last start-pos))
(set 'size (- (last end-pos) (last start-pos)))

(start size (xml-source (chop start-pos)))


We assume that the chopped part of start-pos and end-pos are the same index vector. From you first post in the thread I assume start-pos an end-pos are both 3-element-lists where the first two elements are the same. So basically (xml-source (chop start-pos)) picks the sublist from which we take a slice of.
Title: Re: Selecting part of nested list?
Post by: cormullion on May 07, 2010, 10:11:41 AM
Yes, that's much better!