nil?

Q&A's, tips, howto's
Locked
tom
Posts: 168
Joined: Wed Jul 14, 2004 10:32 pm

nil?

Post by tom »

How do you remove a nil from a list?

m i c h a e l
Posts: 394
Joined: Wed Apr 26, 2006 3:37 am
Location: Oregon, USA
Contact:

Post by m i c h a e l »

Tom,

Would this be what you are looking for?

Code: Select all

> (clean nil? '(1 2 3 nil 4))
(1 2 3 4)
m i c h a e l

tom
Posts: 168
Joined: Wed Jul 14, 2004 10:32 pm

Post by tom »

I thought so. trying it now, that works fine, but I have a list with both empty and nil elements, and clean seems to balk at the empty ones. Here's what I get:

Code: Select all

> box
("" nil "" "sdc1" "sda1" "sda2" "sdb2" "sdb1")
> (clean nil? box)
("" "" "sdc1" "sda1" "sda2" "sdb2" "sdb1")
> (clean empty? box)

ERR: list or string expected in function empty? : nil
>
EDIT

Ok, clean must not be destructive.

Code: Select all

> (set 'box2 (clean nil? box))
("" "" "sdc1" "sda1" "sda2" "sdb2" "sdb1")
> (clean empty? box2)
("sdc1" "sda1" "sda2" "sdb2" "sdb1")
>

m i c h a e l
Posts: 394
Joined: Wed Apr 26, 2006 3:37 am
Location: Oregon, USA
Contact:

Post by m i c h a e l »

We could also cut out the box2 middle-man by using the result of the first function call as the argument to the second function:

Code: Select all

> (set 'box '("" nil "" "sdc1" "sda1" "sda2" "sdb2" "sdb1"))
("" nil "" "sdc1" "sda1" "sda2" "sdb2" "sdb1")
> (clean empty? (clean nil? box))
("sdc1" "sda1" "sda2" "sdb2" "sdb1")
> _
Sorry if you already know this, but someone new to functional programming may not :-)

m i c h a e l

tom
Posts: 168
Joined: Wed Jul 14, 2004 10:32 pm

Post by tom »

:-)
Last edited by tom on Mon Sep 22, 2008 1:41 pm, edited 2 times in total.

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

Or

Code: Select all

> (set 'box '("" nil "" "sdc1" "sda1" "sda2" "sdb2" "sdb1") )
("" nil "" "sdc1" "sda1" "sda2" "sdb2" "sdb1")
> (clean (fn (e) (or (nil? e) (empty? e))) box)
("sdc1" "sda1" "sda2" "sdb2" "sdb1")
> 

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

Or use 'null?' which checks for the nil, the empty list (), the empty string "" and 0 and 0.00 and NaN.

Code: Select all

> (set 'box '("" nil "" "sdc1" "sda1" "sda2" "sdb2" "sdb1"))
("" nil "" "sdc1" "sda1" "sda2" "sdb2" "sdb1")
> (clean null? box)
("sdc1" "sda1" "sda2" "sdb2" "sdb1")
> 

DrDave
Posts: 126
Joined: Wed May 21, 2008 2:47 pm

Post by DrDave »

Lutz wrote:Or use 'null?' which checks for the nil, the empty list (), the empty string "" and 0 and 0.00 and NaN.
Thanks for pointing out 'null?' As you know, I've read the entire user's guide more than once, but didn't recall 'null?'
...it is better to first strive for clarity and correctness and to make programs efficient only if really needed.
"Getting Started with Erlang" version 5.6.2

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

It's hidden between now and nper... :)

DrDave
Posts: 126
Joined: Wed May 21, 2008 2:47 pm

Post by DrDave »

cormullion wrote:It's hidden between now and nper... :)
Ah, maybe I missed it becasue I'm not used to seeing alphabetical ordering such as
'npv'
'nth'
'null?'
'nper' <---(how did this guy cut in line here??)
'number?'
...it is better to first strive for clarity and correctness and to make programs efficient only if really needed.
"Getting Started with Erlang" version 5.6.2

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

DrDave wrote:
cormullion wrote:It's hidden between now and nper... :)
Ah, maybe I missed it becasue I'm not used to seeing alphabetical ordering such as
'npv'
'nth'
'null?'
'nper' <---(how did this guy cut in line here??)
'number?'
This would help to find the queue jumpers, but I think the original is not HTML...?

Code: Select all

(set 'file (read-file "/usr/share/doc/newlisp/newlisp_manual.html"))
(set 'data (find-all (string {<h2><span class="function">([a-z].*?)</span></h2>}) file   $1))

(map (fn (x y) 
  (if (!= x y) 
   (println (format { ? %-18s should be %-18s } x y)) 
   (println (format {   %-18s           %-18s } x y))))
  data
 (sort data))

.....
   nil?                         nil?               
 ? not                should be normal             
 ? normal             should be not                
   now                          now                
 ? null?              should be nper               
 ? nper               should be npv                
 ? npv                should be nth                
 ? nth                should be null?              
   number?                      number?     
.....


Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

The original is in HTML. I updated this copy:

http://www.newlisp.org/downloads/newlisp_manual.html

yesterday and today, and all changes will also show up in 9.9.5

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

Oh yes - it's the PDF that takes the time to generate from the HTML source, not the other way round. Sorry Lutz!

Locked