first or last empty list should return nil

Started by ssqq, September 14, 2016, 06:23:16 PM

Previous topic - Next topic


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.

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


My habit for those cases is to use an or clause, as in
(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
(if @lst (first @lst) -1) It all depends on which access is used, and how "serious" an empty list case is.