implicit indexing on empty string

Q&A's, tips, howto's

implicit indexing on empty string

Postby jef » Mon Oct 13, 2014 7:51 pm

I'm surprised that implicit indexing doesn't raise an error
on an empty string (when called with 0 or -1), while it
does on an empty list:

> ("" 0)
""

> ("" -1)
""

With another index, we get an error as expected:
> ("" 1)

ERR: invalid string index

If we compare to implicit indexing on an empty list, we always
get an error:

> ('() 0)

ERR: invalid list index

>('() -1)

ERR: invalid list index

Any explanation for this?

(btw, thanks again Lutz, newLISP is a real pleasure to use)
jef
 
Posts: 11
Joined: Sun May 18, 2014 8:42 am

Re: implicit indexing on empty string

Postby Lutz » Tue Oct 14, 2014 7:38 am

An empty string is not like an empy list:

Code: Select all
> (push "" "")
""
> (push '() '())
(())
> (atom? "")
true
> (atom? '())
nil
>

an empty list is still a container. The only alternative would be to return nil for ('() 0) - not the symbol nil but the boolaean value nil. This was done in early versions of newLISP. But while programming, it seemed to make more sense to throw an error message.

But still today:
Code: Select all
> (pop '())
nil

... practical for processing lists with pop.

There are other inconsistencies like this in newLISP: the function dup on lists and strings, nil as a boolean value and a symbol, treatment of () and nil - to name a few. When these conflicts occur in newLISP, they are normally decided by the occurrance of real world usage patterns and their practicality rather then trying to be consistent.
Lutz
 
Posts: 5276
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California

Re: implicit indexing on empty string

Postby jef » Tue Oct 14, 2014 6:46 pm

Lutz wrote:When these conflicts occur in newLISP, they are normally decided by the occurrance of real world usage patterns and their practicality rather then trying to be consistent.


Perfect, I see the point. Thanks for your detailed explanation!
jef
 
Posts: 11
Joined: Sun May 18, 2014 8:42 am


Return to newLISP in the real world

Who is online

Users browsing this forum: No registered users and 1 guest

cron