(first '()) anomaly

Started by Kazimir Majorinc, October 15, 2008, 05:45:09 PM

Previous topic - Next topic

Kazimir Majorinc

We have that



(first '())=(first '(nil))=nil and

(rest '())=(rest '(nil))=(), but not



'()='(nil).



It looks like some kind of anomaly.

What others think?

In CL it is similar, in Scheme (first '()) returns error

and it appears more reasonable to me.



I'm sure it was previously discussed, but I wasn't there.
http://kazimirmajorinc.com/\">WWW site; http://kazimirmajorinc.blogspot.com\">blog.

DrDave

#1
Quote from: "Kazimir Majorinc"We have that



(first '())=(first '(nil))=nil and

(rest '())=(rest '(nil))=(), but not



'()='(nil).



It looks like some kind of anomaly.

What others think?

In CL it is similar, in Scheme (first '()) returns error

and it appears more reasonable to me.



I'm sure it was previously discussed, but I wasn't there.


My logical thinking says that if
(first '())
=> nil

then any non-existent condition, whether first or rest, should return the same value, nil.



The question here becomes, does the empty list have a first element or not? The list '(nil) does have a first element, nil. If the empty list does have a first element, then said first element must be nil, because nil is the return value from the evaluation, the same as the return value for '(nil). However, if the the first element of the empty list is just the empty list, then the first value of the empty list is itself.



When I look at the empty list, I see no distinction between the non-existing first element and the non-exisitng rest elements, so expect to see exactly the same value returned, whether it be nil or the empty set or some other arbitrary value. (In this case, my preference is for rest to return nil if there is nothing to return; I think returning the empty list for ANY single-element list is a logic error under the current implementation. However, my REAL preference is to ALWAYS return a list from first or rest; see the end of the post.)



However, it is clear that the definition for rest returns the empty list for every list comprising a single element. So, based on this understanding of rest, the empty list MUST contain a single element, hence must return that element via first, in the same fashion that (first '(nil)) returns that first element, nil. That, therefore, would make the defined return result of nil being incorrect.



In addition, if we accept the above logic that (first '()) returns (), this enables applying the same logical rules to evaluating both (first '()) and (first '(nil)). Then, the return value from (first '()), that APPEARS to be (), has a subtley diffrerent meaning than other instances of () that mean the empty set. In the first case, it is the FIRST ELEMENT of the empty list, while in the second case it IS the empty list. Likewise the evaluaton of the FIRST ELEMENT of the list '(nil) has a sublte diffrenece from the ACTUAL value nil.



But in the end, what I would really prefer is that first and rest ALWAYS return a list, except of course if an error occurs. So, if (length (first list)) or (length (rest list)) return 0, then I want the result of first and rest to return the empty list.  I want to see every result of first returned as a list either with 1 element or else the empty list,  analagous to  the results of rest as a list,
...it is better to first strive for clarity and correctness and to make programs efficient only if really needed.

\"Getting Started with Erlang\"  version 5.6.2

Jeff

#2
That is true in cl but not in newlisp, because and empty list test false but is not == nil in nl.
Jeff

=====

Old programmers don\'t die. They just parse on...



http://artfulcode.net\">Artful code

Kazimir Majorinc

#3
And this also


> (nth 0 '())

ERR: list index out of bounds in function nth
> (first '())
nil
>


First=0th and not first=1st is in my opinion also not the best concept, but it is the problem of whole Lisp and more than half programming world, nothing Newlispecific.
http://kazimirmajorinc.com/\">WWW site; http://kazimirmajorinc.blogspot.com\">blog.

newdep

#4
Actualy, why arnt these 3 below giving the same output?

(as was 'nil in the previous releases..)



I would expect the (first '()) to return ->

ERR: list index is out of bounds in function first





> (first '())



ERR: list is empty in function first : '()



> ('() 0)



ERR: list index out of bounds



> (nth 0 '())



ERR: list index out of bounds in function nth
-- (define? (Cornflakes))

newdep

#5
Now... i though this always worked?



(= '() (empty? '()) )



but it returns 'nil





>(empty? '())

true





Should it be this way? I mean compairing 2 lists this way is logical isnt it?

(for starters this might be confusing... I know its not..)
-- (define? (Cornflakes))

Kazimir Majorinc

#6
Quote from: "newdep"Now... i though this always worked?



(= '() (empty? '()) )



but it returns 'nil


I think it is OK as it is.



Maybe you want something like



(=  (= '() L) (empty? L)) ;=> true?



And it really works that way (except in the edge cases like L=(lambda), it is empty list, but still different than '() and it also has sense).
http://kazimirmajorinc.com/\">WWW site; http://kazimirmajorinc.blogspot.com\">blog.