Index for assoc results

Featuring the Dragonfly web framework
Locked
Tim Johnson
Posts: 253
Joined: Thu Oct 07, 2004 7:21 pm
Location: Palmer Alaska USA

Index for assoc results

Post by Tim Johnson »

From the manual

Code: Select all

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

Re: Index for assoc results

Post by Lutz »

Code: Select all

(find '(1 *) '((2 3) (1 2)) match) => 1
what is it , you want to do with that index number?

Tim Johnson
Posts: 253
Joined: Thu Oct 07, 2004 7:21 pm
Location: Palmer Alaska USA

Re: Index for assoc results

Post by Tim Johnson »

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:

Code: Select all

;; 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
Posts: 253
Joined: Thu Oct 07, 2004 7:21 pm
Location: Palmer Alaska USA

Re: Index for assoc results

Post by Tim Johnson »

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

Code: Select all

(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

Code: Select all

(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:

Code: Select all

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

Re: Index for assoc results

Post by Lutz »

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

Code: Select all

(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
Posts: 253
Joined: Thu Oct 07, 2004 7:21 pm
Location: Palmer Alaska USA

Re: Index for assoc results

Post by Tim Johnson »

Understood.
Thanks
tim
Programmer since 1987. Unix environment.

Locked