Author Topic: (challenge)? ASSOCiate by value vs. key  (Read 4484 times)

0 Members and 1 Guest are viewing this topic.

daron

  • Guest
(challenge)? ASSOCiate by value vs. key
« on: July 12, 2005, 01:32:28 PM »
Take this list:
Code: [Select]
(setq stdWallList '((0 . "Basement") (1 . "Garage") (2 . "Stoop")
    (3 . "Arm") (4 . "Haunch")
    )
  )

and find the associated value of "Stoop".

What I'm looking for is the reverse of:
Code: [Select]
(cdr (assoc 2 stdWallList)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
(challenge)? ASSOCiate by value vs. key
« Reply #1 on: July 12, 2005, 02:00:21 PM »
Code: [Select]
(cadr (assoc "Stoop" (mapcar '(lambda (x) (list (cdr x)(car x))) stdWallList)))
edit - changed to cadr
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

daron

  • Guest
(challenge)? ASSOCiate by value vs. key
« Reply #2 on: July 12, 2005, 02:04:52 PM »
That's good CAB. I came up with:
Code: [Select]
(defun revassoc (itempred itemcomp / newitemlist)
     (foreach item itempred
 (if (= itemcomp (cdr item))
      (setq newitemlist (append newitemlist (list item)))
 )
     )
     newitemlist
)

Not too challenging. I'd like to see the varied possibilities.

whdjr

  • Guest
(challenge)? ASSOCiate by value vs. key
« Reply #3 on: July 12, 2005, 02:18:47 PM »
Code: [Select]
(defun test (str lst)
  (caar (vl-remove-if-not '(lambda (x) (eq (cdr x) str)) lst))
)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
(challenge)? ASSOCiate by value vs. key
« Reply #4 on: July 12, 2005, 02:20:09 PM »
Thanks,
I changed mine to cadr ILO cdr
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

daron

  • Guest
(challenge)? ASSOCiate by value vs. key
« Reply #5 on: July 12, 2005, 02:23:20 PM »
With a small edit, the one I made above can be made to reflect exactly the way assoc does when it encounters multiple key values:
Code: [Select]
;example: (revassoc stdwalllist "Garage")
(defun revassoc (itempred itemcomp / newitemlist)
     (foreach item itempred
 (if (= itemcomp (cdr item))
      (setq newitemlist (append newitemlist (list item)))
 )
     )
     (car newitemlist)
)

My previous one returns a list of all key's equal. This one returns the first of all in just a list, instead of a nested list.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
(challenge)? ASSOCiate by value vs. key
« Reply #6 on: July 12, 2005, 02:37:39 PM »
Here is another.
Code: [Select]
(defun revassoc (itm lst)
  (vl-some '(lambda (x) (if (= (cdr x) itm)(car x))) lst)
)


Did you want the lisy back like this?
Code: [Select]
(defun revassoc (itm lst)
  (vl-some '(lambda (x) (if (= (cdr x) itm) x)) lst)
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
(challenge)? ASSOCiate by value vs. key
« Reply #7 on: July 12, 2005, 02:53:28 PM »
Quote from: CAB
Code: [Select]
(cadr (assoc "Stoop" (mapcar '(lambda (x) (list (cdr x)(car x))) stdWallList)))
edit - changed to cadr

I would keep the use of (cdr) since you are supposed to be using an association list. Which means you need to build the association list in reverse....just a slight mod. to CAB's code:
Code: [Select]
(cdr (assoc "Stoop" (mapcar '(lambda (x) (cons (cdr x)(car x))) stdWallList)))  :D

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
(challenge)? ASSOCiate by value vs. key
« Reply #8 on: July 12, 2005, 03:05:49 PM »
Quote from: Coolhand Luke
What we have here is a failure to communicate.
:shock:
Quote from: daron
What I'm looking for is the reverse of:
Code: [Select]
(cdr (assoc 2 stdWallList))

That doesn't return a list. :D  :D
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
(challenge)? ASSOCiate by value vs. key
« Reply #9 on: July 12, 2005, 03:07:53 PM »
Quote from: CAB
That doesn't return a list. :D  :D
Nor does the code I modified with (cons) in place of (list)  8)  :P

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
(challenge)? ASSOCiate by value vs. key
« Reply #10 on: July 12, 2005, 03:22:11 PM »
Oh, I see what you did. :oops:  The internal result is a dotted pair.
That way it has the same look as the usual code.

How about this:
Code: [Select]
(defun rv_list (lst)
  (mapcar '(lambda (x) (cons (cdr x)(car x))) lst)
)


(cdr (assoc "Stoop" (rv_list stdWallList)))
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
(challenge)? ASSOCiate by value vs. key
« Reply #11 on: July 12, 2005, 03:26:55 PM »
Exactly! I have one of these for switching around layer names for a client with a strange layer naming "standard". The layers are in an assoc list ("MyLayer" . "HisLayer"), etc., then when I want to flip to my layers I just call it (fliplay laylist nil) or to his (fliplay laylist t)....depending the arg. being true I just flip 'em around.

daron

  • Guest
(challenge)? ASSOCiate by value vs. key
« Reply #12 on: July 12, 2005, 04:08:22 PM »
Sorry if there was any miscom. I was trying to leave it a little open. It's good for variations that don't get too far away from the point. It could spur ideas that weren't there to begin with. I should've left out the cdr assoc bit.

SMadsen

  • Guest
(challenge)? ASSOCiate by value vs. key
« Reply #13 on: July 12, 2005, 04:38:51 PM »
Code: [Select]
(defun rvAssoc (item lst)
  (cond ((null lst) nil)
        ((eq (cdar lst) item) (car lst))
        ((rvAssoc item (cdr lst)))
  )
)

daron

  • Guest
(challenge)? ASSOCiate by value vs. key
« Reply #14 on: July 13, 2005, 12:50:20 PM »
Sweet recursion. I had hoped for something like that too. Thanks Stig.