assoc add?

Notices and updates
Locked
hsmyers
Posts: 104
Joined: Wed Feb 20, 2008 4:06 pm
Location: Boise, ID, USA
Contact:

assoc add?

Post by hsmyers »

I noticed the absence of push-assoc while wandering through the manual and of course that raised the obvious question. Probably under the heading of "stupid questions", but how do you add to an associative list? I tried what seemed to be the simple approach:

Code: Select all

> (setq g1 '(("tags" ("Event" "Northern Idaho Open"))("moves" "d4" "f5")("fen")))
(("tags" ("Event" "Northern Idaho Open")) ("moves" "d4" "f5") ("fen"))
> (list? (last g1))
true
> (push "test" (last g1))
"test"
> (last g1)
("fen")
With entirely non obvious results at least to me!

--hsm
"Censeo Toto nos in Kansa esse decisse."—D. Gale "ℑ♥λ"—Toto

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

Aaa..Yes..That one could easily be mistaken by use..
..Because of its naming..

'last => Returns the last element of a list or a string.
(same goes for 'nth and for i.e. (g1 -1)

..It returns the content not its/an index.. (for use by push)

(push "text" g1 -1) will put it to the end of g1

(push "text" g1 -1 -1) pushed even deeper.

(push "text" g1 2 -1) started from the front..
-- (define? (Cornflakes))

hsmyers
Posts: 104
Joined: Wed Feb 20, 2008 4:06 pm
Location: Boise, ID, USA
Contact:

Post by hsmyers »

Ah--- the old copy is not the thing problem ;) Much thanks...

--hsm
"Censeo Toto nos in Kansa esse decisse."—D. Gale "ℑ♥λ"—Toto

hsmyers
Posts: 104
Joined: Wed Feb 20, 2008 4:06 pm
Location: Boise, ID, USA
Contact:

Post by hsmyers »

Don't quite seem to be there yet:

Code: Select all

> (setq g '(("tags" ("Event" "Northern Idaho Open")) ("moves" "d4" "f5") ("fen")))
(("tags" ("Event" "Northern Idaho Open")) ("moves" "d4" "f5") ("fen"))
> (push "x" g 2 -1)
"x"
> g
(("tags" ("Event" "Northern Idaho Open")) ("moves" "d4" "f5") ("x" "fen"))
> (push "xx" g 2 -1)
"xx"
> g
(("tags" ("Event" "Northern Idaho Open")) ("moves" "d4" "f5") ("x" "fen" "xx"))
> (push "xxx" g 2 -1)
"xxx"
> g
(("tags" ("Event" "Northern Idaho Open")) ("moves" "d4" "f5") ("x" "fen" "xx" "xxx"))
The at end nature of -1 seems lost the first time, working there after. Color me confused...

--hsm
p.s. it may not be clear, but each list in g is meant to be an associative list...
"Censeo Toto nos in Kansa esse decisse."—D. Gale "ℑ♥λ"—Toto

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

Yes your right....Thats odd..


> (push "one" T)
("one")

> (push "two" T -1)

("one" "two")

> (push '(three) T -1)

("one" "two" (three))

> (push "four" T -1 -1)

("one" "two" ("four" three))

I would have expected -1 -1 to return ("one" "two" (three "four"))


Looks like it mis-counts when its the first -1 -1
...That looks like a bug...
-- (define? (Cornflakes))

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

Im confused... because I know there where changes regarding
indexing and strings, but if its an Index issue or a string related
issue i cant tell, i still think -1 -1 should be 'end end'.. ;-)


But perhpas Lutz, when he's back in the US, has an explanation for it...



> (set 'T '(("this is a string?")))

> (push "is it?" T -1 -1)

(("is it?" "this is a string?"))



> (set 'T '(( this is not a string?)))

> (push 'isnt? T -1 -1)

((this is not a string? isnt?))
-- (define? (Cornflakes))

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

This is a bug, it should go to the end.

This problem was introduced in 9.2.12, when error messages for overshooting indices where introduced.

It happens when pushing onto a sublist which has exactly one member with -1. The work around would be using a positive index (if the length of the sublist is known).

Code: Select all

> (set 'T '(1 2 (3)))
(1 2 (3))
> (push 4 T -1 1)
4
> T
(1 2 (3 4))
> 
this will be fixed in the development release this weekend. There is also an announcement on the documentation page for 9.3.0 users: http://www.newlisp.org/index.cgi?Documentation

Locked