A custom push-assoc

Started by ghfischer, May 03, 2008, 01:02:31 PM

Previous topic - Next topic

ghfischer

During the assoc discussion I came across a problem I'm sure I knew how to solve a year or two ago, but I can't figure out anymore.



I know I'll only have assoc lists of the form
((a (1 2)) (b (3 4)) ...)

I want to write a function like
(push-assoc key value assoc-lst)

The only way I could get it to work is if I instead had a global assoc-lst that I used.

(set 'alst '())
(define (push-assoc k v )
  (if (lookup k alst)
    (assoc-set (alst k) (list k (append (last $0) (list v))))
    (push (list k (list v)) alst -1)))


Any suggestions on how to make this so I can pass in the assoc-lst ?

I tried using a define-macro but failed.

I think I'm just missing something obvious.

Lutz

#1
Since 9.3.10 (load down the latest development release 9.3.11) you can pass  by reference to any function using the default functor expressed as a context.


; package data in a default functor
(set 'alst:alst '())

(define (push-assoc key value A)
(push (list key value) A))


no you can pass alst:alst by reference just using the namespace identifier:



> (push-assoc 'a 1 alst)
(a 1)
> (push-assoc 'b 2 alst)
(b 2)
> alst:alst
((b 2) (a 1))
>


on 9.3.0 this would only work on functions of this form: (... (L idx) ...), now it works on any function taking a list or string.



ps: you can just take your function and add the additional parameter, and it will work this way