first or last empty list should return nil

For the Compleat Fan
Locked
ssqq
Posts: 88
Joined: Sun May 04, 2014 12:49 pm

first or last empty list should return nil

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

ralph.ronnquist
Posts: 228
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia

Re: first or last empty list should return nil

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

Locked