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
Searching elements in an association list...
-
- Posts: 28
- Joined: Thu Jan 13, 2005 12:42 am
- Location: California
- Contact:
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)) )
> (magoo "chocolate bar" food-lists)
("junk food" "ice cream" "licorice" "chocolate bar" "carmel apple" "bubble gum")
Improved so that (magoo "no such food" food-lists) returns nil.
> (magoo "no such food" food-lists)
nil
> (magoo "celery" food-lists)
("vegatables" "lettuce" "celery" "cucumbers" "olives" "onions")
Code: Select all
(define (magoo key aList)
(let
( vec (ref key aList) )
;body of let
(and vec (aList (vec 0)) ) ))
nil
> (magoo "celery" food-lists)
("vegatables" "lettuce" "celery" "cucumbers" "olives" "onions")