Search through a list of alists -- bug found?

Started by Cyril, April 16, 2010, 02:33:26 PM

Previous topic - Next topic

Cyril

I am trying to search through a list of the associative lists (a sort of nested context handling). I have tried to use a call to assoc function as a break condition of dolist, example code follows:


newLISP v.10.2.1 on Win32 IPv4, execute 'newlisp -h' for more info.

> ; a sort of nested contexts
> (setq la '(((k0 v0) (k1 v1)) ((k2 v2) (k3 v3)) ((k4 v4) (k5 v5))))
(((k0 v0) (k1 v1)) ((k2 v2) (k3 v3)) ((k4 v4) (k5 v5)))

> ; we can search through them
> (dolist (x la) (println $idx ": " x ", " (assoc 'k3 x)))
0: ((k0 v0) (k1 v1)), nil
1: ((k2 v2) (k3 v3)), (k3 v3)
2: ((k4 v4) (k5 v5)), nil
nil

> ; but when we try to stop...
> (dolist (x la (assoc 'k3 x)) (println $idx ": " x))
0: ((k0 v0) (k1 v1))
?

> ; something weird already! If we try to save the result...
> (setq result (dolist (x la (assoc 'k3 x)) (println $idx ": " x)))


... the interpreter crashes!



A bug in my code, or in newLISP?
With newLISP you can grow your lists from the right side!

johu

#1
newLISP v.10.2.1 on Win32 IPv4, execute 'newlisp -h' for more info.

> (setq la '(((k0 v0) (k1 v1)) ((k2 v2) (k3 v3)) ((k4 v4) (k5 v5))))
(((k0 v0) (k1 v1)) ((k2 v2) (k3 v3)) ((k4 v4) (k5 v5)))
> (dolist (x la (assoc 'k3 x)) (println $idx ": " x))
0: ((k0 v0) (k1 v1))
?
> (dolist (x la (lookup 'k3 x)) (println $idx ": " x))
0: ((k0 v0) (k1 v1))
?
> (setq la2 (explode (flat la) 2))
((k0 v0) (k1 v1) (k2 v2) (k3 v3) (k4 v4) (k5 v5))
> (dolist (x la2 (match '(k3 ?) x)) (println $idx ": " x))
0: (k0 v0)
1: (k1 v1)
2: (k2 v2)
(v3)
> (dolist (x la (setq result (assoc 'k3 x))) (println $idx ": " x))
0: ((k0 v0) (k1 v1))
(k3 v3)
> (dolist (x la (setq result (lookup 'k3 x))) (println $idx ": " x))
0: ((k0 v0) (k1 v1))
v3
> (dolist (x la (copy (assoc 'k3 x))) (println $idx ": " x))
0: ((k0 v0) (k1 v1))
(k3 v3)
> (dolist (x la (copy (lookup 'k3 x))) (println $idx ": " x))
0: ((k0 v0) (k1 v1))
v3
>


Possibly, there seems to be any problem of assoc and lookup.



For the time being, copy can be used to avoid the interpreter crashes.

Lutz

#2
Incidentally a similar problem was reported to me by email on Thursday and it is fixed in v.10.2.4. The source package is posted here:



http://www.newlisp.org/downloads/development/">http://www.newlisp.org/downloads/development/



This was a problem with all looping primitives, which have a local variables.

Cyril

#3
Quote from: "Lutz"Incidentally a similar problem was reported to me by email on Thursday and it is fixed in v.10.2.4


newLISP v.10.2.4 on Win32 IPv4, execute 'newlisp -h' for more info.

> (setq la '(((k0 v0) (k1 v1)) ((k2 v2) (k3 v3)) ((k4 v4) (k5 v5))))
(((k0 v0) (k1 v1)) ((k2 v2) (k3 v3)) ((k4 v4) (k5 v5)))

> (dolist (x la (assoc 'k3 x)) (println $idx ": " x))
0: ((k0 v0) (k1 v1))
(k3 v3)


Thanks, Lutz!
With newLISP you can grow your lists from the right side!