Page 1 of 1

first or last empty list should return nil

Posted: Thu Sep 15, 2016 1:23 am
by ssqq
When `rest` or `chop` or `filter` a list, empty list would be create.
But many function that process list would throw error with empty list (first last rest nth).
So newlisper need treate empty list as special list to avoid code crash.

If index empty list return nil, then would simplify coding processing list.

Code: Select all

(set '@lst (process @lst))
(cond
   ((symbol? (first @lst) (dosth ..))
   (true ..))

Re: first or last empty list should return nil

Posted: Fri Sep 16, 2016 1:15 am
by ralph.ronnquist
My habit for those cases is to use an or clause, as in

Code: Select all

(first (or @lst '(-1)))
Thus, I use a second or term to provide an appropriate sentinel construct to make the access provide whatever value I want the empty list to result in. And sometimes it works better with an if clause, as in

Code: Select all

(if @lst (first @lst) -1)
It all depends on which access is used, and how "serious" an empty list case is.