Searching elements in an association list...

Pondering the philosophy behind the language
Locked
Sunburned Surveyor
Posts: 28
Joined: Thu Jan 13, 2005 12:42 am
Location: California
Contact:

Searching elements in an association list...

Post by Sunburned Surveyor »

I would like to know if there is a function in NewLISP that will return all the elements of an association list based on the value of any of the lists members.

For example, I have the following list:

("fruit" "bannana" "pear" "apple" "plum" "grapes")
("vegatables" "lettuce" "celery" "cucumbers" "olives" "onions")
("junk food" "ice cream" "licorice" "chocolate bar" "carmel apple" "bubble gum")

I want to be able to retrieve the entire "junkfood" list by searching for any one of the elements. I believe the lookup and assoc functions only allow you to do this when using the first element in the association list.

Thanks for the help.

The Sunburned Surveyor

Sammo
Posts: 180
Joined: Sat Dec 06, 2003 6:11 pm
Location: Loveland, Colorado USA

Post by Sammo »

Code: Select all

(set 'food-lists
'(
  ("fruit" "bannana" "pear" "apple" "plum" "grapes") 
  ("vegatables" "lettuce" "celery" "cucumbers" "olives" "onions") 
  ("junk food" "ice cream" "licorice" "chocolate bar" "carmel apple" "bubble gum") 
))

(define (magoo findme a-list)
  (a-list ((ref findme a-list) 0)) )
Then

> (magoo "chocolate bar" food-lists)
("junk food" "ice cream" "licorice" "chocolate bar" "carmel apple" "bubble gum")

Sammo
Posts: 180
Joined: Sat Dec 06, 2003 6:11 pm
Location: Loveland, Colorado USA

Post by Sammo »

Improved so that (magoo "no such food" food-lists) returns nil.

Code: Select all

(define (magoo key aList)
  (let
    ( vec (ref key aList) )
  ;body of let
    (and vec (aList (vec 0)) ) ))
> (magoo "no such food" food-lists)
nil
> (magoo "celery" food-lists)
("vegatables" "lettuce" "celery" "cucumbers" "olives" "onions")

Locked