Index for assoc results

Started by Tim Johnson, March 09, 2010, 09:06:40 AM

Previous topic - Next topic

Tim Johnson

From the manual
(assoc 1 '((3 4) (1 2)))  → (1 2)

Is there a system variable that stores the index/position of '(1 2)?

And if not,

1)What is the best way to get the index?

2):) Put it on _my_ wish list.

thanks

tim
Programmer since 1987. Unix environment.

Lutz

#1
(find '(1 *) '((2 3) (1 2)) match) => 1

what is it , you want to do with that index number?

Tim Johnson

#2
From my previous programming experience, I've had many needs for the index of a search result.

I'm aware that in newlisp one takes more of a functional approach, but it seems to me

that the execution of 'assoc in the native C code would have some sort of offset recorded, thus

it would be an additional (and valuable) resource to have the offset (index) provided in a system variable.

The specific case that prompts me to ask this question is where I would have an "associative" list whose

members would need to be

1)accessed

2)Any number of possible logical branches executed based on the contents of the member.

3)changed

I've written this:

;; With 'all, return indexes for all occurances of 'key in 'lst. Without 'all return the index of the first occurance.
(define (getAssocPos key lst all)
(letn((cmp (fn(x) (= (x 0) key)))
(res (index cmp lst)))
(if (empty? res)
nil
(if all
res
(first res)))))

Thus (I think) getAssocPos will allow me to retrieve, then store an offset/index which will reference a member-list

and then allow me to easily reset it in the outer list via 'setf.

I hope this answers your question.

Thanks for the 'find example. I have use for that.

cheers

tim
Programmer since 1987. Unix environment.

Tim Johnson

#3
OK. Let me take another crack at this: I will use examples that are not tested code but are meant to be illustrative:

I have a list, let's call it 'parent-list. It is composed of lists, and we will call them 'member-list(s). They can be

accessed by 'assoc
(set 'member-list (assoc key parent-list))
;; all sorts of code including possible changes to 'member-list
----
----
(setf (assoc key parent-list) member-list)  ;; we had to call 'assoc a second time. Redundant?

Suppose we had a $found system variable

(set 'member-list (assoc key parent-list)
      'found $found)
;; all sorts of code including possible changes to 'member-list
----
----
(setf (parent-list found) member-list)  ;; I'm assuming the Implicit indexing is more efficient

With the function I wrote, I could do something like this:
(letn ((found getAssocPos(key lst)) (member-list (lst found))
 (when found
  ;; all sorts of code including possible changes to 'member-list
----
----
  )
(setf (parent-list found) member-list) ;; implicit indexing  
) ;; end letn

That's all for today.

G'night

tim
Programmer since 1987. Unix environment.

Lutz

#4
In the 'setf' expression you could use the '$it' system variable in a more complex function:


(define (transform-found-assoc item)
...
)

(setf (assoc key parent-list)
(transform-found-assoc $it))


'$it' contains the association found and the 'transform-found-assoc' function does all the work required, then at last returns the new changed item and 'parent-list' is changed.

Tim Johnson

#5
Understood.

Thanks

tim
Programmer since 1987. Unix environment.