Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

help me with this function

1 view
Skip to first unread message

Kevin P Chugh

unread,
Mar 23, 1996, 3:00:00 AM3/23/96
to
i'm trying to write a function that takes two arguments - one is an ato
the other is a list of lists - for each list within the list, if the
atom matches its first memeber, i want it's second member to be added
to a master list and finally returned- for example:

(setq mylist '((a b)(a c)(a d)))
(myfunc 'a lst)

and myfunc would return

(b c d)

i've tried using with-collection and collect but i can't get them to work
properly- anyone have any ideas?

thanks,
kevin

Pete Grant

unread,
Mar 23, 1996, 3:00:00 AM3/23/96
to
On Mar 23, 1996 02:50:29 in article <help me with this function>,
(defun myfunc (item list)
(mapcan #'(lambda (x)
(and (eql item (car x))
(cdr x)))
list))

If on a Symbolics machine (may work with others also),

(defun myfunc (item list)
(loop for (key data) in list
when (eql item key)
collect data))

--

Pete Grant
Kalevi, Inc.
Sofware Engineering

Erik Naggum

unread,
Mar 24, 1996, 3:00:00 AM3/24/96
to
[Kevin P Chugh]

| i'm trying to write a function that takes two arguments - one is an

| atom the other is a list of lists - for each list within the list, if


| the atom matches its first memeber, i want it's second member to be
| added to a master list and finally returned- for example:
|
| (setq mylist '((a b)(a c)(a d)))
| (myfunc 'a lst)
|
| and myfunc would return
|
| (b c d)
|
| i've tried using with-collection and collect but i can't get them to work
| properly- anyone have any ideas?

the body of a function with args car and list could be quite simple:

(mapcan (lambda (cons) (if (eq (car cons) car) (cdr cons))) list)

it is more instructive for you to write a recursive function. you may note
that (myfunc car list) is identical to (myfunc car (cdr list)) if (car list)
does not match car. you get the idea.

#<Erik>
--
in my next life, I want to be my cat

Kenny Tilton

unread,
Mar 26, 1996, 3:00:00 AM3/26/96
to ch...@cs.buffalo.edu
I do not know of a one-step solution, but you could try:

(defun cullCar (match lists)
(delete-if #'null (mapcar #'(lambda (list)
(when (eql match (car list))
(cadr list)))
lists)))

This would be inefficent where many non-matches occur. Doesn't the loop
macro have some neat features for this? (Don't use it myself.)

0 new messages