nil?

Started by tom, September 20, 2008, 09:39:07 PM

Previous topic - Next topic

tom

How do you remove a nil from a list?

m i c h a e l

#1
Tom,



Would this be what you are looking for?


> (clean nil? '(1 2 3 nil 4))
(1 2 3 4)


m i c h a e l

tom

#2
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:



> 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.



> (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

#3
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:


> (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

#4
:-)

cormullion

#5
Or


> (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

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


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

DrDave

#7
Quote from: "Lutz"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

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

DrDave

#9
Quote from: "cormullion"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

#10
Quote from: "DrDave"
Quote from: "cormullion"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...?



(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

#11
The original is in HTML. I updated this copy:



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



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

cormullion

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