Search through a list of alists -- bug found?

Notices and updates
Locked
Cyril
Posts: 183
Joined: Tue Oct 30, 2007 6:27 pm
Location: Moscow, Russia
Contact:

Search through a list of alists -- bug found?

Post by 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:

Code: Select all

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
Posts: 143
Joined: Mon Feb 08, 2010 8:47 am

Re: Search through a list of alists -- bug found?

Post by johu »

Code: Select all

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
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: Search through a list of alists -- bug found?

Post by Lutz »

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/

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

Cyril
Posts: 183
Joined: Tue Oct 30, 2007 6:27 pm
Location: Moscow, Russia
Contact:

Re: Search through a list of alists -- bug found?

Post by Cyril »

Lutz wrote:Incidentally a similar problem was reported to me by email on Thursday and it is fixed in v.10.2.4

Code: Select all

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!

Locked