Composing a multi-level 'assoc call

Started by Tim Johnson, May 04, 2008, 06:31:39 PM

Previous topic - Next topic

Tim Johnson

When I execute the following:
(set 'lst '(
    (id001 (name "Anne") (addr (country "USA") (city "New York")))
    (id002 (name "Jean") (addr (country "France") (city "Paris")))
))
(set 'myargs '('id002 'addr))
;; ('id002 'addr)
(set 'allargs (append '(lst) myargs))
;; (lst 'id002 'addr)
(apply assoc allargs)

I get list expected in function assoc : ''id002
Whereas
(assoc (lst 'id002 'addr))
Yields the expected result.

What is the correct way?

Thanks

Tim
Programmer since 1987. Unix environment.

Lutz

#1
When you have multiple keys only the second parenthesized form will work. The flat from and using 'apply' would only work for one key:


(set 'allargs (list 'id001 lst))

(apply assoc allargs)

=> (id001 (name "Anne") (addr (country "USA") (city "New York")))


In case you have to construct the arguments for the parenthesized multi-key capable form you could do it this way:


(set 'allargs '((lst 'id002 'addr)))

(eval (cons assoc allargs))

 => (addr (country "France") (city "Paris"))


But why would you do this, when all elements in the parenthesized form can be variables:


(set 'key1 'id002)
(set 'key2 'addr)

(assoc (lst key1 key2))

=> (addr (country "France") (city "Paris"))

Tim Johnson

#2
Hi Lutz.

My goal is an interface to a context that manages associative lists

of any level.

Thus, rather than use named variables, I'd like to use (args)

- so think (args) instead of myargs.

Unfortunately, I still can't wrap my brain around _that_ approach.

thanks

tim
Programmer since 1987. Unix environment.

Tim Johnson

#3
Draft attempt to use more of a 'drill-down' approach
(define (hashit)
  (catch
   (let((res lst))
(doargs (i)
(set 'res (assoc i res))
(if (not res) (throw nil))) ;; dead end
(throw res)) ;; done
   'res ;; catch it here
   ) res) ;; return here
Programmer since 1987. Unix environment.