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

How to remove nil from a list?

88 views
Skip to first unread message

Mike

unread,
May 18, 2012, 1:43:21 PM5/18/12
to
Hi,

I'm trying to remove a pair from a list. So far I have:

(setq y1 '((#:X14 19) (TF TG) (A 5) (B 6) (C 7) (D 8)))
=> ((#:X14 19) (TF TG) (A 5) (B 6) (C 7) (D 8))
(map 'list #'(lambda (p) (if (not (eq (car p) 'c)) p)) y1)
=> ((#:X14 19) (TF TG) (A 5) (B 6) NIL (D 8))

I'm getting a nil in my returned list. I don't want the nil. What am
I not doing right so that I get the nil? I understand that 'map wants
to put something there when processing the list 'y1. I prefer to have
the pair for 'C removed and not be in the returning list.

I'm getting better with loops and have started using the collect
clause. (yah!)

Mike

Stefan Mandl

unread,
May 18, 2012, 1:52:47 PM5/18/12
to
What you are trying to do is filtering not mapping.
The respective built-in functions go under names
like remove-if-not. Function names like that make me shudder ;-)

Good luck!

Stefan

Teemu Likonen

unread,
May 18, 2012, 2:09:59 PM5/18/12
to
* Mike [2012-05-18 17:43:21 +0000] wrote:

> I'm trying to remove a pair from a list. So far I have:
>
> (setq y1 '((#:X14 19) (TF TG) (A 5) (B 6) (C 7) (D 8)))
> => ((#:X14 19) (TF TG) (A 5) (B 6) (C 7) (D 8))
> (map 'list #'(lambda (p) (if (not (eq (car p) 'c)) p)) y1)
> => ((#:X14 19) (TF TG) (A 5) (B 6) NIL (D 8))
>
> I'm getting a nil in my returned list. I don't want the nil.

CL-USER> (remove 'c '((#:X14 19) (TF TG) (A 5) (B 6) (C 7) (D 8))
:key #'car)

((#:X14 19) (TF TG) (A 5) (B 6) (D 8))

Mike

unread,
May 18, 2012, 2:26:52 PM5/18/12
to
Duh. Thanks.

Mike

Mike

unread,
May 18, 2012, 2:31:54 PM5/18/12
to
Stefan,

So 'map (and 'mapcar, etc) are transformational.

Thanks

Mike

Teemu Likonen

unread,
May 18, 2012, 2:34:31 PM5/18/12
to
* Mike [2012-05-18 18:26:52 +0000] wrote:

> Duh. Thanks.

You're welcome. Peter Seibel's "Practical Common Lisp" a good book to
learn the language:

http://www.gigamonkeys.com/book/

Barry Margolin

unread,
May 18, 2012, 3:03:38 PM5/18/12
to
If you really want to do this with a mapping function, you can use
MAPCAN. Instead of making a list of all the results, it expects the
function to return a list, and concatenates all the results. What your
filtering function has to do then is return NIL for the elements it
wants to remove, and wrap the elements that stay in an extra level of
list struction.

(mapcan #'(lambda (p) (if (eq (car p) 'c) nil (list p))) y1)

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

WJ

unread,
May 18, 2012, 9:29:08 PM5/18/12
to
Is it possible? A user of CL who knows something besides
LOOP? Who isn't merely a COBOL programmer?

Alberto Riva

unread,
May 19, 2012, 5:19:28 PM5/19/12
to
In that case you can easily accomplish what you want with LOOP:

(loop
for p in y1
unless (eq (car p) 'c)
collect p)

Alberto

--
Give a man a fish and he will eat for one day. Teach him programming,
and he will develop a multi tasking intelligent planning algorithm to
optimize the route of a fleet of deep sea fishing boats, with
real-time GPS tracking, weather satellite link, automatic analysis of
fish market prices, the whole thing controlled by an AJAX-enabled web
interface. In the meantime, he will die of hunger.

Marco Antoniotti

unread,
May 21, 2012, 4:15:15 AM5/21/12
to
The built-in functions are named remove
The built-in functions are named remove, remove-if, delete and delete-if. True you also have the 'if-not' versions. Of course, you may think that you only need a 'filter' function (or maybe a 'fil' function), but then you need either a more complex or a more B&D or a more complex and B&D compiler to deal with all the corner cases.

Cheers
--
Marco

Marco Antoniotti

unread,
May 21, 2012, 4:16:56 AM5/21/12
to
Some people instead claim to hate loop and traditional iteration constructs, not realizing they are themselves in a cycle (of posting and re-posting the same stuff) :)

Cheers
--
MA
0 new messages