Assoc & Lookup

For the Compleat Fan
Locked
newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Assoc & Lookup

Post by newdep »

Hello Lutz,

In the philosophy of Assoc and Lookup the search in nested-list have only the first element as 'key-index. In lists I think all elements are key-indexes
to eighother..thats more logical..? I know..there should be always and index
if more possebilites exist, but in that case always the first is returned.

Searching for the second or third value in a nested list could result
in the following..

(setq X '( ( 1 2 ) ( 3 4 ) ( 5 6 ) ( 7 8 9 ) ))

(assoc 8 X)
>( 7 8 9 )

(lookup 4 X)
> 3

(lookup 8 X)
> 7 true 9 or 7 9

(lookup 7 X)
> true 8 9 or 8 9


Regards, Norman.
-- (define? (Cornflakes))

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

Post by newdep »

Mmmm i think this would impact everything in newlisp, I solved it
personaly using a double / tripple nested list where every element
is the index..

Norman.
-- (define? (Cornflakes))

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

Post by Lutz »

perhaps (ref ...) will do it for you:

(setq X '( ( 1 2 ) ( 3 4 ) ( 5 6 ) ( 7 8 9 ) ))

(ref 8 X) => (3 1)

It gives you a multidimensional index into X, which could have any nesting complexity you want.

You could then extract that element or just look at it:

(pop X '(3 1)) => 8 ; change X

(nth 3 1 X) => 8; leave X how it is

X => ((1 2) (3 4) (5 6) (7 9))

and put something else back:

(push 88 X '(3 1))

X => ((1 2) (3 4) (5 6) (7 88 9))

because 'push' and 'pop' can take multiple indices and they may be in a list, so the result from 'ref' can directly be used in push/pop.

Ref can take any expression as a search key.

(ref '(3 4) X) => (1)

(nth 1 X) => (3 4)


Lutz

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

Post by newdep »

HA !..nice nice... thanks...
-- (define? (Cornflakes))

Locked