newLISP Fan Club

Forum => newLISP newS => Topic started by: cormullion on November 26, 2008, 11:52:46 AM

Title: match and set-ref-all stack overflow
Post by: cormullion on November 26, 2008, 11:52:46 AM
Is there another way to do this - I'm getting a stack overflow in match for values over 1300...


(for (i 0 1400)
   (push (list (string i) i) results -1))
(set 'l (last (first  results)))
(set-ref-all '(+ +)  results (list (first $it) (- (last $it) l))  match)


newLISP v.9.9.95
Title:
Post by: Lutz on November 26, 2008, 01:13:04 PM
I took care of the stack overflow in 'set-ref-all' with 'match' for 9.9.96.



Meanwhile I suggest the following work-around:


(replace '(+ +)  results (list (first $it) (- (last $it) l))  match)


replace/match does not grow the stack.



Not sure why you have chosen 'set-ref-all' for that type of data. You would use 'set-ref-all', if the pattern you are looking for '(+ +) can be found on different nesting levels. In your example we deal with a flat list of lists.



But in case you real data does have more complex nesting you could do this:


(set 'refs (ref-all '(+ +) results match))
(dolist (vec refs)
(setf (results vec) (list (first $it) (- (last $it) l))))


in '(results vec)' you are indexing with an index vector list, its a nice example how the result from 'ref' or 'ref-all' can be used for multi-dimensional indexing



ps: note that in your data l (letter l) was zero, so the replacement didn't really change. I plugged in 1 (number one) to make sure all is working and gives no stack overflow.
Title:
Post by: cormullion on November 26, 2008, 02:47:43 PM
Cool - thanks.



Choosing the right function is the key thing - newLISP has many cool functions now, working with different flavours of lists. And I forgot that I could use match with replace... :)