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.
(first '()) anomaly
-
- Posts: 388
- Joined: Thu May 08, 2008 1:24 am
- Location: Croatia
- Contact:
Re: (first '()) anomaly
My logical thinking says that ifKazimir Majorinc wrote: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.
Code: Select all
(first '())
=> 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
"Getting Started with Erlang" version 5.6.2
-
- Posts: 388
- Joined: Thu May 08, 2008 1:24 am
- Location: Croatia
- Contact:
And this also
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.
Code: Select all
> (nth 0 '())
ERR: list index out of bounds in function nth
> (first '())
nil
>
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
(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))
-
- Posts: 388
- Joined: Thu May 08, 2008 1:24 am
- Location: Croatia
- Contact:
I think it is OK as it is.newdep wrote:Now... i though this always worked?
(= '() (empty? '()) )
but it returns 'nil
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).