first or last empty list should return nil

For the Compleat Fan

first or last empty list should return nil

Postby ssqq » Thu Sep 15, 2016 1:23 am

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 ..))
ssqq
 
Posts: 88
Joined: Sun May 04, 2014 12:49 pm

Re: first or last empty list should return nil

Postby ralph.ronnquist » Fri Sep 16, 2016 1:15 am

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.
ralph.ronnquist
 
Posts: 178
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia


Return to Anything else we might add?

Who is online

Users browsing this forum: No registered users and 1 guest

cron