newLISP Fan Club

Forum => newLISP newS => Topic started by: Kazimir Majorinc on October 15, 2008, 05:45:09 PM

Title: (first '()) anomaly
Post by: Kazimir Majorinc on October 15, 2008, 05:45:09 PM
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.
Title: Re: (first '()) anomaly
Post by: DrDave on October 16, 2008, 02:01:42 AM
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,
Title:
Post by: Jeff on October 16, 2008, 05:47:31 AM
That is true in cl but not in newlisp, because and empty list test false but is not == nil in nl.
Title:
Post by: Kazimir Majorinc on October 16, 2008, 01:58:29 PM
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.
Title:
Post by: newdep on October 28, 2008, 02:32:54 AM
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
Title:
Post by: newdep on October 28, 2008, 02:35:41 AM
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..)
Title:
Post by: Kazimir Majorinc on October 28, 2008, 03:10:43 AM
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).