Why The Difference In Prepend vs. Append?

431 views
Skip to first unread message

Onorio Catenacci

unread,
Nov 5, 2014, 9:31:50 AM11/5/14
to elixir-l...@googlegroups.com
Hi all,

Just curious about something I spotted recently:

iex(1)> L1 = [1,2,3]
[1, 2, 3]

iex(2)> L2 = [4|L1]
[4, 1, 2, 3]                #Prepend behaves as expected

iex(3)> L3 = [L1|4]      
[[1, 2, 3] | 4]             #Append behaves differently.

Is this due to the nature of a linked list (that is O(N) complexity for an append vs. O(1) for a prepend)?  Not that I have a need to append to a list but I am curious about the difference in the behavior.

--
Onorio


Onorio Catenacci

unread,
Nov 5, 2014, 9:51:57 AM11/5/14
to elixir-l...@googlegroups.com
By the way in my original code I was using lowercase "L"; I changed the case of the variables after I copy/pasted them into my message to make it easier to distinguish between 1 and L here.  Of course if you try that code at an iex prompt it won't work. Obviously not: an uppercase L is an atom (like a module name).  Sorry for my carelessness. As I say, I was just trying to avoid confusion between lowercase L and 1. 

If you use lowercase var names the sample code behaves as I mentioned. Just wanted to save anyone telling me that, of course, L<n> won't work for binding a list to a variable.

--
Onorio

Peter Hamilton

unread,
Nov 5, 2014, 9:59:19 AM11/5/14
to elixir-l...@googlegroups.com

That is not append, it is prepend in both cases.

[a|b] is shorthand for prepend. Erlang (and many lisps) do not b to be a list. The result of prepending an element to an element is called an improper list. Most list manipulating functions assume a proper list and will break with an improper list, but the language itself does t prevent you from creating them anyway.

In the second case, we are prepending an element (a list) to another element (a single value), creating an improper list.

Arie van Wingerden

unread,
Nov 5, 2014, 9:59:40 AM11/5/14
to elixir-l...@googlegroups.com
Hi Onorio,

when you do [ 4 | lst ] variable lst is a list and 4 is added to the front of that list.

When you do [ lst | 4 ] you create a so called "improper list", because 4 is NOT a list and thus you cannot add something to it's front!

General rule (in Erlang and Elixir): avoid improper lists. So prepend always to an existing LIST.

In Lisp and derivatives sometimes improper lists have a good use as pairs. This is not the case for Erlang & Elixir.

Regards,
   Arie

--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Lars Storjord

unread,
Nov 5, 2014, 12:03:48 PM11/5/14
to elixir-l...@googlegroups.com
Just a small side note: Prepending to a list like that is often called 'cons'ing, after the cons function in Lisp. That is handy to know in case you read more about it in other contexts online.

Onorio Catenacci

unread,
Nov 5, 2014, 2:37:55 PM11/5/14
to elixir-l...@googlegroups.com
Thanks Peter, Arie and Lars for your responses.


--
Onorio

Reply all
Reply to author
Forward
0 new messages