assoc add?

Started by hsmyers, April 24, 2008, 12:36:33 PM

Previous topic - Next topic

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:
> (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 \"[size=117]ℑ♥λ[/size]\"—Toto

newdep

#1
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

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



--hsm
\"Censeo Toto nos in Kansa esse decisse.\"—D. Gale \"[size=117]ℑ♥λ[/size]\"—Toto

hsmyers

#3
Don't quite seem to be there yet:
> (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 \"[size=117]ℑ♥λ[/size]\"—Toto

newdep

#4
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

#5
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

#6
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).


> (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">http://www.newlisp.org/index.cgi?Documentation