[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Chicken-users] list vs dotted-list
From: |
Ivan Shmakov |
Subject: |
Re: [Chicken-users] list vs dotted-list |
Date: |
Thu, 11 Nov 2010 22:14:22 +0600 |
User-agent: |
Gnus/5.11 (Gnus v5.11) Emacs/22.2 (gnu/linux) |
>>>>> Yi DAI <address@hidden> writes:
> Hi, On a recent consideration of the design of Scheme, a question
> comes to my mind: Why do we have both list and dotted-list, given
> that the former is just a special case (terminated by nil) of the
> latter? Can we simply live with the latter? I see it is fairly easy
> to write common list procedures, like `length', `map', `append' etc
> that could handle dotted-list very well, with the predicate
> `pair?'. Is there some special consideration for the 'terminating a
> list by nil' convention? (I see one maybe from the recursive data
> structure view, but it does not convince me.)
That's simple: we'd just check what one could do with these
dotted-lists alone.
Suppose, e. g., that one wants to put some tiny integers into a
dotted-list; so one does:
(cons 1 2)
=> (1 . 2)
Does it work? Quite so. Let's move on to something harder.
What if one wants a two-element dotted-list, whose first element
is, in turn, a dotted-list? One then may:
(cons (cons 1 2) 3)
=> ((1 . 2) . 3)
Looks like a viable solution: the inner dotted-list is clearly
separated from the outer. But what if we've to do it the other
way around: can we construct a two-element dotted-list whose
/second/ elements is a dotted list? Let's see...
(cons 1 (cons 2 3))
=> (1 2 . 3)
Unfortunately, we can't distinguish a two element dotted list,
whose second element is, in turn, a dotted list, from a three
(or more) element dotted list.
> If 'terminating a list by nil' is considered good, can we live
> without `dot', having `cons' build a true list (conforming to the
> definition of list in recursive data structure)? In other words, is
> it a good idea to have everything be a list or dotted-list?
We can't spare CONS for LIST, either, as it makes impossible to
build lists step by step. There's a fundamental difference in
these constructs:
(cons 1 LST) ; make a list by prepending 1 to LST;
(list 1 LST) ; make a list by encasing 1 /and/ LST.
So, if LST is, e. g., (2 3 4), a three-element list, then:
(cons 1 LST)
=> (1 2 3 4) ; a four-element list;
while
(list 1 LST)
=> (1 (2 3 4)) ; a two-element list.
Of course, one may point that (append (list ELT) LST) is
essentially the same as (cons ELT LST), but it's just a
consequence of APPEND being implemented /in terms of/ CONS.
Then, the only question is whether one wants to have a simple
operation, and to use it to define a more complex one, or to
have the more complex one from the beginning.
The spirit of Scheme, AIUI, is to do the former.
--
FSF associate member #7257
pgpeBUt80dOKf.pgp
Description: PGP signature