Page 1 of 1

nil?

Posted: Sun Sep 21, 2008 4:39 am
by tom
How do you remove a nil from a list?

Posted: Sun Sep 21, 2008 5:53 am
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

Posted: Sun Sep 21, 2008 2:44 pm
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")
>

Posted: Sun Sep 21, 2008 5:30 pm
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

Posted: Sun Sep 21, 2008 5:48 pm
by tom
:-)

Posted: Sun Sep 21, 2008 5:49 pm
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")
> 

Posted: Sun Sep 21, 2008 7:56 pm
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")
> 

Posted: Mon Sep 22, 2008 12:19 pm
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?'

Posted: Mon Sep 22, 2008 3:30 pm
by cormullion
It's hidden between now and nper... :)

Posted: Mon Sep 22, 2008 5:01 pm
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?'

Posted: Tue Sep 23, 2008 4:03 pm
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?     
.....


Posted: Tue Sep 23, 2008 4:35 pm
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

Posted: Tue Sep 23, 2008 4:40 pm
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!