View unanswered posts | View active topics It is currently Thu Sep 09, 2010 4:46 am



Post new topic Reply to topic  [ 6 posts ] 
Index for assoc results 
Author Message

Joined: Thu Oct 07, 2004 7:21 pm
Posts: 248
Location: Palmer Alaska USA
Post Index for assoc results
From the manual
Code:
(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


Tue Mar 09, 2010 5:06 pm
Profile E-mail

Joined: Thu Sep 26, 2002 4:45 pm
Posts: 4159
Location: Boca Raton, Florida
Post Re: Index for assoc results
Code:
(find '(1 *) '((2 3) (1 2)) match) => 1


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


Wed Mar 10, 2010 1:01 am
Profile E-mail WWW

Joined: Thu Oct 07, 2004 7:21 pm
Posts: 248
Location: Palmer Alaska USA
Post Re: Index for assoc results
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:
;; 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


Wed Mar 10, 2010 2:24 am
Profile E-mail

Joined: Thu Oct 07, 2004 7:21 pm
Posts: 248
Location: Palmer Alaska USA
Post Re: Index for assoc results
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:
(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:
(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:
(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


Wed Mar 10, 2010 4:01 am
Profile E-mail

Joined: Thu Sep 26, 2002 4:45 pm
Posts: 4159
Location: Boca Raton, Florida
Post Re: Index for assoc results
In the 'setf' expression you could use the '$it' system variable in a more complex function:

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


Wed Mar 10, 2010 12:21 pm
Profile E-mail WWW

Joined: Thu Oct 07, 2004 7:21 pm
Posts: 248
Location: Palmer Alaska USA
Post Re: Index for assoc results
Understood.
Thanks
tim


Wed Mar 10, 2010 4:30 pm
Profile E-mail
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Forum style by Vjacheslav Trushkin for Free Forums/DivisionCore.