Re: Adding to the end of a list



Bill Spight wrote:
> Dear Brian,
>
> Brian Hulley wrote:
> >
> > Anders Lindén wrote:
> > > (Just like you add to a list in the beginning with [A|List]?
> >
> > No - [A|List] is a new list whose head is A and whose tail is List
> > (nothing is added to List)
>
> That depends upon your definition of 'add'. E. g., if you add 1 to 2
you
> get 3. However, 2 remains the same, it does not become 3 when you add
1
> to it. Similarly, when you add Head to Tail to get [Head | Tail],
Tail
> remains the same.

Yes (also in reply to Torkel's post) thanks for pointing these things
out. I posted the comment because I thought that perhaps the original
question (by Anders) might have been due to a misconception that
[A|List] was some syntax for destructively modifying a list in the same
way that List.push_front(A) in STL would add A to the head of List in
C++ for example, because if so you would then expect an equally neat
equivalent for push_back.

Given that this may not have been the motivating thought for the
question, a better answer from me to Anders might have been that a list
is not a flat structure like an array with two ends, but is rather a
nested structure which has an outside and an inside.

ie a list has the form [a, [b, [c, [d, [..... []]]]]]

Clearly you can "add" something outside (by including this list in a
bigger enclosing list) but you can't add anything inside because
there's no space to add anything there (but difference lists (Torkel's
post) give you this "inner space")

Hence the asymmetry between "adding at the beginning" and "adding at
the end"

Regards, Brian.

.



Relevant Pages

  • Re: How to get the terms of a compund term (predicate: simplfy)?
    ... > to show the real format of one expressions, ... expression into two lists and then recombining the lists ... if bla bla Head is something bla bla ... ... that Head and Tail variables is not instantiated and ...
    (comp.lang.prolog)
  • Re: I can not install FreeBSD 5.3 in an old Pentium 100 MHz
    ... Brian Bobowski wrote: ... to be prepared to install it as soon as disc arrived. ... I am used to write in the Debian mailing lists and I am used to answer ...
    (freebsd-questions)
  • Re: How to remove redundant elements of a list
    ... I know the algorithm for remove the same elements,but I do not know ... list into head and tail components, consider head, and then recurse ... lists usually has the form: ...
    (comp.lang.ml)
  • Re: Linked List
    ... file_collate_2.c is interesting because it has a list of lists. ... ** with some same purchases as other files. ... head = tail = NULL; ... ** (head) list format: ...
    (comp.lang.c)
  • Re: Converting Scheme to Prolog
    ... element from the head of a list. ... The third case covers lists embedded within lists (this is the tricky ... and delete from the tail. ...
    (comp.lang.prolog)